C++:(a<<b) 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10983078/
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
C++: what does (a<<b) mean?
提问by itamar
I have a C++ header file that contains the following definitions:
我有一个包含以下定义的 C++ 头文件:
#define CACHE_NUM_WAYS (1<<1)
#define CACHE_DATA_SIZE (1<<8)
It is used as an integer in the rest of the code.
它在其余代码中用作整数。
What does it mean? And what is its value?
这是什么意思?它的价值是什么?
回答by John Humphreys - w00te
1 << 1 means:
1 << 1 表示:
00000000 00000001 changes to 00000000 00000010
1 << 8 means:
1 << 8 表示:
00000000 00000001 changes to 00000001 00000000
It's a bit shift operation. For every 1 on the right, you can think of yourself as multiplying the value on the left by 2. So, 2 << 1 = 4 and 2 << 2 = 8. This is much more efficient than doing 1 * 2.
这是一个位移操作。对于右边的每一个 1,你可以认为自己将左边的值乘以 2。所以,2 << 1 = 4 和 2 << 2 = 8。这比做 1 * 2 效率高得多。
Also, you can do 4 >> 1 = 2 (and 5 >> 1 = 2 since you round down) as the inverse operation.
此外,您可以执行 4 >> 1 = 2 (并且 5 >> 1 = 2 因为您向下取整)作为逆运算。
回答by Ran
Those are bitwise shift operators.
这些是按位移位运算符。
http://msdn.microsoft.com/en-us/library/336xbhcz(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/336xbhcz(v=vs.80).aspx
<< is shifting left so it is actually multiplying by 2 for << 1 and by 2^8 for << 8.
<< 向左移动,所以它实际上乘以 2 表示 << 1 和乘以 2^8 表示 << 8。
回答by moonshadow
a<<b
for integers means "shift left". The bitwise representation of a
is shifted left b
bits. This is the same as multiplying by (2 to the power of b
).
a<<b
对于整数意味着“左移”。的按位表示a
左移b
。这与乘以 (2 的 次方b
) 相同。
So in your example, (1<<1)
is 1*(2^1)
is 2
, (1<<8)
is 1*(2^8)
is 256
.
所以在你的例子中,(1<<1)
是1*(2^1)
是2
,(1<<8)
是1*(2^8)
是256
。
It is worth pointing out that in general, as with other operators in c++, <<
may be overridden to perform other functions. By default, input/output streams override this operator to let you write concise code to send a bunch of parameters to the stream. So you may see code like this:
值得指出的是,一般来说,与 c++ 中的其他运算符一样,<<
可能会被覆盖以执行其他功能。默认情况下,输入/输出流会覆盖此运算符,让您编写简洁的代码来向流发送一堆参数。所以你可能会看到这样的代码:
cout << something << somethingelse
and <<
does notmean left shift in this context.
并且<<
也并不意味着在这种情况下左移。
回答by Hymandoe
<<
is bitwise shift left (there is also >>
bitwise shift right)
if you have 32 bit integer
<<
>>
如果您有 32 位整数,则按位左移(也有按位右移)
1 = 00000000 00000000 00000000 00000001 = 1
1 << 1 = 00000000 00000000 00000000 00000010 = 2
1 << 8 = 00000000 00000000 00000001 00000000 = 256
回答by Nawaz
The operator <<
is a bitwise left-shift operator.
运算符<<
是按位左移运算符。
So when you write 1<<17
, the binary representation of 1
is shifted left by 17
bits as:
因此,当您编写 时1<<17
, 的二进制表示1
按17
位左移,如下所示:
//before (assume 1 is represented by 32-bit)
1 << 17
0000 0000 0000 0000 0000 0000 0000 0001 << 17 (before - binary representation)
//after
0000 0000 0000 0010 0000 0000 0000 0000 (after - binary representation)