C++ 什么是 (x & 1) 和 (x >>= 1)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38922606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is (x & 1) and (x >>= 1)?
提问by Sandra K
I am trying to do assignment: "Find the number of bits in an unsigned integer data type without using the sizeof() function."
我正在尝试分配:“在不使用 sizeof() 函数的情况下查找无符号整数数据类型中的位数。”
And my design is to convert the integer to bits and then to count them. For ex: 10 is 1010
and 5 is 101
我的设计是将整数转换为位,然后对它们进行计数。例如:10 is 1010
和5 is 101
Converting integer to a bit representationshows something like this:
将整数转换为位表示显示如下:
do
{
Vec.push_back( x & 1 )
}
while ( x >>= 1 );
I don't want to just copy paste stuff. When I use F-10 I see what (x & 1)
is doing but I don't know it is name or how it does its job(compare something?). Also I know >=
which "greater than or equal" but what is x >>= 1
?
我不想只是复制粘贴的东西。当我使用 F-10 时,我看到(x & 1)
在做什么,但我不知道它的名字或它是如何工作的(比较一下?)。我也知道>=
哪个“大于或等于”但什么是x >>= 1
?
Note: The marked duplicate is a JavaScript and not C++
注意:标记的重复项是 JavaScript 而不是 C++
采纳答案by dasblinkenlight
These are Bitwise Operators(reference).
这些是按位运算符(参考)。
x & 1
produces a value that is either 1
or 0
, depending on the least significant bit of x
: if the last bit is 1
, the result of x & 1
is 1
; otherwise, it is 0
. This is a bitwise AND operation.
x & 1
产生一个值为1
或的值0
,具体取决于 的最低有效位x
: 如果最后一位是1
,则结果x & 1
是1
;否则,它是0
。这是一个按位与操作。
x >>= 1
means "set x
to itself shifted by one bit to the right". The expression evaluates to the new value of x
after the shift.
x >>= 1
表示“设置x
为自身向右移动一位”。该表达式计算为x
移位后的新值。
Note:The value of the most significant bit after the shift is zero for values of unsigned type. For values of signed type the most significant bit is copied from the sign bit of the value prior to shifting as part of sign extension, so the loop will never finish if x
is a signed type, and the initial value is negative.
注意:对于无符号类型的值,移位后的最高有效位的值为零。对于有符号类型的值,在作为符号扩展的一部分移位之前,从值的符号位复制最高有效位,因此如果x
是有符号类型,则循环永远不会完成,并且初始值为负。
回答by rama
x & 1
is equivalent to x % 2
.
x & 1
相当于x % 2
。
x >> 1
is equivalent to x / 2
x >> 1
相当于 x / 2
So, these things are basically the result and remainder of divide by two.
所以,这些东西基本上是除以二的结果和余数。
回答by stackomatiker
In addition to the answer of "dasblinkenlight" I think an example could help. I will only use 8 bits for a better understanding.
除了“dasblinkenlight”的答案,我认为一个例子会有所帮助。为了更好地理解,我将只使用 8 位。
x & 1
produces a value that is either1
or0
, depending on the least significant bit ofx
: if the last bit is1
, the result ofx & 1
is1
; otherwise, it is0
. This is a bitwise AND operation.
x & 1
产生一个值为1
或的值0
,具体取决于 的最低有效位x
: 如果最后一位是1
,则结果x & 1
是1
;否则,它是0
。这是一个按位与操作。
This is because 1
will be represented in bits as 00000001
. Only the last bit is set to 1
. Let's assume x
is 185
which will be represented in bits as 10111001
. If you apply a bitwise AND operation on x
with 1
this will be the result:
这是因为1
将以位表示为00000001
。只有最后一位设置为1
。让我们假设x
is185
将以位表示为10111001
。如果你申请的按位与运算x
用1
这将是结果:
00000001
10111001
--------
00000001
The first seven bits of the operation result will be 0
after the operation and will carry no information in this case (see Logical AND operation). Because whatever the first seven bits of the operand x
were before, after the operation they will be 0
. But the last bit of the operand 1
is 1
and it will reveal if the last bit of operand x
was 0
or 1
. So in this example the result of the bitwise AND operation will be 1
because our last bit of x
is 1
. If the last bit would have been 0
, then the result would have been also 0
, indicating that the last bit of operand x
is 0
:
运算结果的前七位将在运算0
之后,在这种情况下将不携带任何信息(请参阅逻辑与运算)。因为无论操作数x
的前七位之前是什么,在操作之后它们都会是0
. 但是操作数的最后一位1
是1
,它会显示操作数的最后一位x
是0
或1
。所以在这个例子中,按位与运算的结果将是1
因为我们的最后一位x
是1
。如果最后一位是0
,那么结果也是0
,表明操作数的最后一位x
是0
:
00000001
10111000
--------
00000000
x >>= 1
means "setx
to itself shifted by one bit to the right". The expression evaluates to the new value ofx
after the shift
x >>= 1
表示“设置x
为自身向右移动一位”。表达式计算为x
移位后的新值
Let's pick the example from above. For x >>= 1
this would be:
让我们从上面选择示例。对于x >>= 1
这将是:
10111001
--------
01011100
And for left shift x <<= 1
it would be:
对于左移x <<= 1
,它将是:
10111001
--------
01110010
Please pay attention to the note of user "dasblinkenlight" in regard to shifts.
请注意用户“dasblinkenlight”关于班次的说明。
回答by Satyendra Yadav
It is similar to x = (x >> 1)
.
它类似于x = (x >> 1)
.
(operand1)(operator)=(operand2) implies(=>) (operand1)=(operand1)(operator)(operand2)
It shifts the binary value of x by one to the right.
它将 x 的二进制值向右移动一位。
E.g.
例如
int x=3; // binary form (011)
x = x >> 1; // zero shifted in from the left, 1 shifted out to the right:
// x=1, binary form (001)