简体   繁体   English

C++: 什么 (a< <b) mean?< div><div id="text_translate"><p> 我有一个 C++ header 文件,其中包含以下定义:</p><pre> #define CACHE_NUM_WAYS (1<<1) #define CACHE_DATA_SIZE (1<<8)</pre><p> 它在代码的rest中用作integer。</p><p> 这是什么意思? 它的价值是什么?</p></div></b)>

[英]C++: what does (a<<b) mean?

I have a C++ header file that contains the following definitions:我有一个 C++ header 文件,其中包含以下定义:

#define CACHE_NUM_WAYS    (1<<1)
#define CACHE_DATA_SIZE   (1<<8)

It is used as an integer in the rest of the code.它在代码的rest中用作integer。

What does it mean?这是什么意思? And what is its value?它的价值是什么?

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,因为向下舍入)作为反向操作。

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。

<< 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

a<<b for integers means "shift left". a<<b表示整数表示“向左移”。 The bitwise representation of a is shifted left b bits. 的按位表示a左移b位。 This is the same as multiplying by (2 to the power of 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. 值得指出的是,通常,与c ++中的其他运算符一样, <<可以被覆盖以执行其他功能。 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: 所以你可能会看到这样的代码:

cout << something << somethingelse

and << does not mean left shift in this context. << 并不意味着在这种情况下左移。

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)

In CPP, "<<" stands for the left-shift operator and ">>" stands for the right-shift operator.在 CPP 中,“<<”代表左移运算符,“>>”代表右移运算符。

x<<y is equivalent to multiplying x with 2^y and x>>y is equivalent to dividing x by 2^y. x<<y 相当于 x 乘以 2^y,x>>y 相当于 x 除以 2^y。

In theoretical terms, the left shift operator shifts the bits of the first operand to the left and the operand on the right in this case decides the number of bits to shift to the left and similarly, the right shift operator shifts the bits of the first operand to the right and the operand on the right decides the number of bits to shift to the right.从理论上讲,左移运算符将第一个操作数的位向左移动,在这种情况下,右侧的操作数决定向左移动的位数,类似地,右移运算符将第一个操作数的位移动右边的操作数和右边的操作数决定向右移动的位数。

1<<8 1<<8

here is how i understood it.这是我的理解方式。 in the most layman way, understand it as 1 (00000001) in binary number system is now promoted to the 8 places(bits) ahead from where it was earlier, and for every place as we know it moves the value(2 in binary) gets exponentially multiplied so it becomes 1*(2^8)=256.以最外行的方式,将其理解为二进制数系统中的 1 (00000001) 现在被提升到它之前的 8 个位置(位),并且对于我们所知的每个位置,它都会移动值(二进制中的 2)得到指数倍乘,所以它变成 1*(2^8)=256。

so 1<<6 will be 1*(2^6)= 64 and so on所以 1<<6 将是 1*(2^6)= 64 等等

a<<b means (a * pow(2,b))-> a * 2^b // left-sift operator << a<<b表示(a * pow(2,b))-> a * 2^b //左筛选运算符 <<

a>>b means (a / pow(2,b))-> a / 2^b // right-sift operator >> a>>b表示(a / pow(2,b))-> a / 2^b //右筛运算符 >>

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

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