What does (x>>1)&1 result?

I’m working with bits and I don’t understand this operator.

unsigned int number = 6;
number = (number>>1)&1;

The resulting value in number is 1 and I don’t know why. 6 is 0110 in binary. If I shift 1 position to the right the result is 0011, which is 3. The value in number now is 1, which is the last bit in 0011. Does the operation return the last digit??



First you do a right shift operation. It shifts each bit in its left operand to the right. The number following the operator decides the number of places the bits are shifted.

0110 >> 1 = 0011

Then you perform a bitwise AND operation, which does a logical AND of the bits in each position of a number in its binary form.

& 0001
= 0001

So yes, if you perform a bitwise AND operation with 1, the result is going to be the last digit of the left operand.