(a

时间:2023-02-08 21:40:33

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?

这是什么意思?它的价值是什么?

5 个解决方案

#1


18  

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,因为你四舍五入)作为反运算。

#2


5  

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

#3


3  

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

#4


2  

The operator << is a bitwise left-shift operator.

运算符 <是位左移运算符。< p>

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)

#5


1  

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++中的其他操作符一样,< <可能被重写以执行其他功能。默认情况下,输入 输出流覆盖此操作符,使您能够编写简洁的代码,向流发送一系列参数。你可能会看到这样的代码:< p>

cout << something << somethingelse

and << does not mean left shift in this context.

<不表示左移。< p>

#1


18  

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,因为你四舍五入)作为反运算。

#2


5  

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

#3


3  

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

#4


2  

The operator << is a bitwise left-shift operator.

运算符 <是位左移运算符。< p>

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)

#5


1  

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++中的其他操作符一样,< <可能被重写以执行其他功能。默认情况下,输入 输出流覆盖此操作符,使您能够编写简洁的代码,向流发送一系列参数。你可能会看到这样的代码:< p>

cout << something << somethingelse

and << does not mean left shift in this context.

<不表示左移。< p>