This question already has an answer here:
这个问题在这里已有答案:
- What are bitwise shift (bit-shift) operators and how do they work? 8 answers
- 什么是按位移位(位移)运算符以及它们如何工作? 8个答案
I am working on a simple project in C where I encountered this statement in one of the tutorials.
我正在使用C中的一个简单项目,我在其中一个教程中遇到了这个语句。
int i = 1 << 2 ;
What does this statement actually do?
这个陈述实际上做了什么?
4 个解决方案
#1
2
This is also called the shift operator. It will shift de bits n positions to the left. Shifting the bits by one positions is the same as multiplying or dividing by 2.
这也称为移位运算符。它会将位n位置向左移位。将位移位一个位置与乘以或除以2相同。
#2
2
It is a left-shift operator.
这是一个左移运营商。
It applies on the bit representation of 1 and shift it's bits to left by 2 position.
它适用于1的位表示,并将其位向左移位2位。
#3
0
<<
is a binary Left Shift operator. The left operands value is shifted left by the number of bits specified by the right operand. and 0
are padded in the right. So the binary of 1
i.e, 1
is shifted left 2 places and becomes 100
which is 4
in decimal number system. So i
is assigned 4
.
< <是二元左移运算符。左操作数值向左移动右操作数指定的位数。和0填充在右边。因此,1,1的二进制数向左移位2位,变为100,十进制数系统为4。所以我被分配了4。< p>
Here is a quick tutorial that explains bitwise operations
这是一个解释按位操作的快速教程
#4
0
Shift left is a bitwise operation. Look here: Bitwise operations in C wiki
左移是一个按位操作。看这里:C维基中的按位运算
#1
2
This is also called the shift operator. It will shift de bits n positions to the left. Shifting the bits by one positions is the same as multiplying or dividing by 2.
这也称为移位运算符。它会将位n位置向左移位。将位移位一个位置与乘以或除以2相同。
#2
2
It is a left-shift operator.
这是一个左移运营商。
It applies on the bit representation of 1 and shift it's bits to left by 2 position.
它适用于1的位表示,并将其位向左移位2位。
#3
0
<<
is a binary Left Shift operator. The left operands value is shifted left by the number of bits specified by the right operand. and 0
are padded in the right. So the binary of 1
i.e, 1
is shifted left 2 places and becomes 100
which is 4
in decimal number system. So i
is assigned 4
.
< <是二元左移运算符。左操作数值向左移动右操作数指定的位数。和0填充在右边。因此,1,1的二进制数向左移位2位,变为100,十进制数系统为4。所以我被分配了4。< p>
Here is a quick tutorial that explains bitwise operations
这是一个解释按位操作的快速教程
#4
0
Shift left is a bitwise operation. Look here: Bitwise operations in C wiki
左移是一个按位操作。看这里:C维基中的按位运算