How am I getting a single bit from an int?

bit-manipulation, bitwise-operators, c#

Solution

Unless my boolean algebra from school fails me, what's happening should be equivalent to the following:

              *
  1100110101101  // last bit is 1
& 0000000000001  // & 1
= 0000000000001  // = 1

              *
  1100110101100  // last bit is 0
& 0000000000001  // & 1
= 0000000000000  // = 0

So when you do `& 1`, what you're basically doing is to zero out all other bits except for the last one which will remain whatever it was. Or more technically speaking you do a bitwise AND operation between two numbers, where one of them happens to be a `1` with all leading bits set to `0`

Problem

I understand that: int bit = (number >> 3) & 1; Will give me the bit 3 places from the left, so lets say 8 is 1000 so that would be 0001. What I don't understand is how "& 1" will remove everything but the last bit to display an output of simply "1". I know that this works, I know how to get a bit from an int but how is it the code is extracting the single bit? Code... ``` int number = 8; int bit = (number >> 3) & 1; Console.WriteLine(bit); ```

Original source