Can someone explain how 1 & -1 = 1? How does the compiler perform binary operations with negative numbers?
Just write the binary representation of both numbers. Let’s pretend we are working with 3 bit signed integers:
1 (decimal) = 001 (binary)
-1 (decimal) = 111 (binary)
1 & -1 = 001 & 111 = 001
Works the same for 32 bit signed integers. The leftmost bit represents the sign. 0 for positive and 1 for negative. So the result of negative number & positive number will always be a positive number. It will only be negative if and only if both arguments are negative.
Any thing that is not zero is a TRUE when you talk about boolean value
Number = 0001 ,
(-1 ) = 2’s complement = 1111 ,
0001
&1111 = 0001