What’s mean by numer & (-number) ?
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)
本文深入探讨了二进制数的补码表示方法,并通过具体例子展示了如何使用位运算计算负数的补码形式。特别地,文章解释了如何通过按位取反加一得到负数的补码,以及如何利用位与运算找到二进制数最右侧的1位。
956

被折叠的 条评论
为什么被折叠?



