Let me assume that negative value is represented using two’s complement.
In this case, -i
can be calculated as (~i)+1
(flip bits, then add 1).
For example, let me consider i = 44
. Then, in binary,
i = 0000 0000 0000 0000 0000 0000 0010 1100
~i = 1111 1111 1111 1111 1111 1111 1101 0011
-i = (~i)+1 = 1111 1111 1111 1111 1111 1111 1101 0100
(i) & (-i) = 0000 0000 0000 0000 0000 0000 0000 0100
As you see, the least bit that is 1 can be calculated using (i) & (-i)
.