Why the bit operation i & (-i) equals to rightmost bit?

天大地大妈咪最大 提交于 2019-11-29 12:51:57

Say you've got a two's complement binary number i:

0b1101001010000000

and you want to find -i. Well, -i is the number such that i + (-i) == 0. So what number has that property? Well, if you construct another number:

 i: 0b1101001010000000
-i: 0b0010110110000000

such that the rightmost set bit is the same as in i, all bits after that are 0, and all bits before that are the inverse of those in i:

 i: 0b11010010 1 0000000
-i: 0b00101101 1 0000000

then when you add these numbers together, the carries propagate off the left side of the number and just leave all 0 bits, so this is -i.

Now, what do we get if we & these numbers? Well, the trailing zeros & together to produce zeros. The bits on the left are opposites in i and -i, so they & together to produce zeros. But the rightmost set bit is 1 in both i and -i, so that's the only set bit in i & -i.

     i: 0b11010010 1 0000000
    -i: 0b00101101 1 0000000
i & -i: 0b00000000 1 0000000

It even works for negative integers, it doesn't really matter, we can prove it for bitstrings in general.

First the i != 0 case:

Using string notation,

-(a10k) = (~a)10k (either by definition, or by working out -x = ~x + 1)

Note that a number that isn't 0 is always in the form a10k, that is, it start with "anything", then has a rightmost 1 followed by any number of zeroes (maybe zero zeroes).

Then,

a10k & (~a)10k = 10k ('a' cancels with its inverse)

For the 0 case, well, there is no rightmost 1 so it isn't in the result either.

When you decrement a two's complement integer, you:

  1. Find the right-most 1 bit and set it to 0; and
  2. Set all the lower-order bits to 1

i-1, therefore, has all the 1 bits that i has, except the right-most one.

The complement, ~(i-1), therefore shares no 1 bits with i, except he rightmost one, so i & ~(i-1) contains only the right-most 1 bit in i.

This can be simplified a bit if we note that ~x = -x-1, so ~(i-1) = -(i-1)-1 = -i. So the right-most 1 bit in i is just i&-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!