 # binary operation

int x= 6 & -6 ; then x= 2
how does the bitwise operation take place ? ANd how in -6 represented in binary form ?

Perhaps this could help. Take a look at the second answer: it’ll make things clear.

Six is represented as 110 in base 2 . That’s simple right . It can changed back into decimal form by :
0 * 2^0 + 1 * 2^1 + 1 * 2^2 .
Also note that there are preceding or leading zeroes in the binary representation of 6 .
So exact representation is 000…000110
For representation of a negative number , just represent the number as if the negative sign is not there . Then subtract one from it . Remember you don’t need a representation of negative 0 , as negative 0 and positive zero is the same thing . So subtracting 1 is always possible . After subtraction take the bitwise complement of the number . Now you have the representation of the negative number .
So for -6 we have
0000…00110 ( ignoring the sign )
Then subtract one to get ,
0000…00101
Then take bitwise complement to get ,
1111…111010

The bitwise operations happens exactly as you would expect them , just doing the operation A op B for the i’th bit in A and i’th bit in B and storing the result in i’th bit in result .

4 Likes

int(16 bits)

6 --> 0000000000000110

-6 --> 1111111111111010 (2’s complement of(6))

(0000000000000110)
AND(&)(1111111111111010)

ans is
2 --> 0000000000000010