简体   繁体   English

按位和(&)正数和负数的含义?

[英]Meaning of bitwise and(&) of a positive and negative number?

Can anyone help what n&-n means?? 谁能帮助n&-n意味着什么? And what is the significance of it. 它的意义何在?

It's an old trick that gives a number with a single bit in it, the bottom bit that was set in n . 这是一个老技巧,它给出了一个带有一个位的数字,它是在n中设置的最低位。 At least in two's complement arithmetic, which is just about universal these days. 至少在二进制补码算法中,这几天几乎是普遍的。

The reason it works: the negative of a number is produced by inverting the number, then adding 1 (that's the definition of two's complement). 它起作用的原因:数字的负数是通过反转数字,然后加1(这是二进制补码的定义)产生的。 When you add 1, every bit starting at the bottom that is set will overflow into the next higher bit; 当你加1时,从底部开始的每个位都会溢出到下一个更高的位; this stops once you reach a zero bit. 一旦达到零位,这就会停止。 Those overflowed bits will all be zero, and the bits above the last one affected will be the inverse of each other, so the only bit left is the one that stopped the cascade - the one that started as 1 and was inverted to 0. 那些溢出的位都将为零,并且受影响的最后一位之上的位将是彼此相反的位,因此剩下的唯一位是停止级联的位 - 从1开始并且被反转为0的位。

PS If you're worried about running across one's complement arithmetic here's a version that works with both: PS如果你担心运行一个补码运算,这里有一个适用于这两个版本的版本:

n & (~n + 1)

几乎每个系统都是大多数人真正关心的,它会给你2的最高功率,n可以被整除。

It's just a bitwise-and of the number. 这只是一个位数和数字。 Negative numbers are represented as two's complement . 负数表示为二进制补码

So for instance, bitwise and of 7&(-7) is x00000111 & x11111001 = x00000001 = 1 因此,例如,按位和7&( - 7)是x00000111&x11111001 = x00000001 = 1

N&(-N) will give you position of the first bit '1' in binary form of N . N&(-N)将以二进制形式N给出一位'1'位置。 For example: 例如:

N = 144 (0b10010000) => N&(-N) = 0b10000
N = 7 (0b00000111) => N&(-N) = 0b1

One application of this trick is to convert an integer to sum of power-of-2. 这个技巧的一个应用是将整数转换为2的幂之和。 For example: 例如:

To convert 22 = 16 + 4 + 2 = 2^4 + 2^2 + 2^1
22&(-22) = 2, 22 - 2 = 20
20&(-20) = 4, 20 - 4 = 16
16&(-16) = 16, 16 - 16 = 0

我相信这是一个弄巧是n是2的幂的技巧。(n ==(n&-n))IFF n是2(1,2,4,8)的幂。

I would add a self-explanatory example to the Mark Randsom 's wonderful exposition. 我想在Mark Randsom的精彩阐述中加入一个不言自明的例子。

010010000 | +144 ~
----------|-------
101101111 | -145 +
        1 |
----------|-------
101110000 | -144 

101110000 | -144 & 
010010000 | +144
----------|-------
000010000 |   16`

Because x & -x = {0, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 32} for x from 0 to 32. It is used to jumpy in the for sequences for some applications. 因为x & -x = {0, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 32}x为0〜32它是用来跳动在用于某些应用的序列。 The applications can be to store accumulated records. 应用程序可以存储累积的记录。

for(;x < N;x += x&-x) {
    // do something here
    ++tr[x];
}

The loop traverses very fast because it looks for the next power of two to jump. 循环遍历非常快,因为它寻找下一个跳跃的2的幂。

As @aestrivex has mentioned, it is a way of writing 1.Even i encountered this 正如@aestrivex所提到的,这是一种写作方式1.即使我遇到了这个

for (int y = x; y > 0; y -= y & -y)

and it just means y=y-1 because 它只是意味着y = y-1因为
7&(-7) is x00000111 & x11111001 = x00000001 = 1 7&( - 7)是x00000111&x11111001 = x00000001 = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM