C++ 什么是 1 << 0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18215681/
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 1 << 0?
提问by CodeDoctorJL
enum
{
kFlag_FPS = 1 << 0,
kFlag_Help = 1 << 1,
kFlag_RedBlue3D = 1 << 2,
}
I am trying to understand what this code is I don't quite know what:
我试图理解这段代码是什么我不太清楚:
1 << 0
means?
方法?
Any help is greatly appreciated!
任何帮助是极大的赞赏!
回答by Caesar
From MSDN - Shift Operators: >> and <<
The left-shift operator causes the bit pattern in the first operand to be shifted to the left by the number of bits specified by the second operand. Bits vacated by the shift operation are zero-filled. This is a logical shift instead of a shift-and-rotate operation.
左移运算符使第一个操作数中的位模式向左移动第二个操作数指定的位数。由移位操作腾出的位用零填充。这是逻辑移位而不是移位和旋转操作。
This means that the user is taking the bits value of 1
and shifting the bits to the left based on the right number.
这意味着用户正在获取 的位值1
并根据正确的数字向左移动位。
That means that in this case, their values will look like this in binary.
这意味着在这种情况下,它们的值在二进制中看起来像这样。
1 << 0 = `0000 0001`
1 << 1 = `0000 0010`
1 << 2 = `0000 0100`
The first shift is not necessary, but it looks more consistent with the rest.
第一个班次不是必需的,但它看起来与其他班次更一致。
回答by 1''
1 << 0
is 1 shifted to the left by 0 positions, which is just 1.
1 << 0
是 1 向左移动 0 个位置,也就是 1。
回答by Victor.Palyvoda
x << y
- means shift bits of x to the left (to larger value) y times.
In math, this looks like:
x * (2^y)
or x * pow(2, y)
x << y
- 表示将 x 的位向左移动(到更大的值)y 次。在数学中,这看起来像:
x * (2^y)
或x * pow(2, y)
回答by sedavidw
The <<
operator is a bit shifter. So 1 << 2
, is equal to 4
as you take 1
and shift by 2
bits. When using 1 << 0
, that has no impact on the value and is probably there to make everything appear consistent
该<<
操作是移位器。所以1 << 2
, 等于4
你取1
并按位移2
位。使用时1 << 0
,这对值没有影响,并且可能使所有内容看起来一致
回答by Ted Hopp
It could have been simply
本来可以很简单
enum
{
kFlag_FPS = 1,
kFlag_Help = 1 << 1,
kFlag_RedBlue3D = 1 << 2,
}
but the coder likes more symmetry.
但编码员喜欢更多的对称性。
回答by Ping Woo
>> (Signed right shift)
if the number is negative, then 1 is used as a filler and if the number is positive, then 0 is used as a filler.
>> (Signed right shift)
如果数字为负,则使用 1 作为填充符,如果数字为正,则使用 0 作为填充符。
int x = -4;
System.out.println(x>>1); //output -2
int y = 4;
System.out.println(y>>1); //output 2
>>> (Unsigned right shift)
In Java, the operator ‘>>>' is unsigned right shift operator. It always fills 0 irrespective of the sign of the number.
// x is stored using 32 bit 2's complement form.
// Binary representation of -1 is all 1s (111..1)
>>> (Unsigned right shift)
在 Java 中,运算符“>>>”是无符号右移运算符。无论数字的符号如何,它始终填充 0。// x 使用 32 位 2 的补码形式存储。
// -1 的二进制表示全为 1 (111..1)
int x = -1;
System.out.println(x>>>29); // The value of 'x>>>29' is 00...0111
System.out.println(x>>>30); // The value of 'x>>>30' is 00...0011
System.out.println(x>>>31); // The value of 'x>>>31' is 00...0001