我注意到我可以用2 << 5来得到64,用1000 >> 2来得到250。

我也可以在打印中使用>>:

print >>obj, "Hello world"

这里发生了什么?


当前回答

它们是存在于许多主流编程语言中的位移位运算符,<<是左移,>>是右移,它们可以演示如下表,假设一个整数只占用内存1字节。

| operate | bit value | octal value |                       description                        |
| ------- | --------- | ----------- | -------------------------------------------------------- |
|         | 00000100  |           4 |                                                          |
| 4 << 2  | 00010000  |          16 | move all bits to left 2 bits, filled with 0 at the right |
| 16 >> 2 | 00000100  |           4 | move all bits to right 2 bits, filled with 0 at the left |

其他回答

它们是存在于许多主流编程语言中的位移位运算符,<<是左移,>>是右移,它们可以演示如下表,假设一个整数只占用内存1字节。

| operate | bit value | octal value |                       description                        |
| ------- | --------- | ----------- | -------------------------------------------------------- |
|         | 00000100  |           4 |                                                          |
| 4 << 2  | 00010000  |          16 | move all bits to left 2 bits, filled with 0 at the right |
| 16 >> 2 | 00000100  |           4 | move all bits to right 2 bits, filled with 0 at the left |

这些是位移位运算符。

从文件中引用:

x << y

返回x,其中的位向左移动了y位(右边的新位为零)。这就等于x乘以2**y。

x >> y

返回x,位向右移动y位。这和x除以2**y是一样的。

这些是移位运算符

x << y返回x,其中比特向左移动y位(和 右边的新位是零)。这和 x乘以2**y。 x >> y返回x,位移位到 右移y位。这和//'ing x * 2* y是一样的。

<< Mean any given number will be multiply by 2the power
for exp:- 2<<2=2*2'1=4
          6<<2'4=6*2*2*2*2*2=64

12 is less than < 2 48

当我们执行上面的语句时,12的实际二进制值是“00 1100”。左移(左移2位)返回值48,其二进制值是“11 0000”。

48 >> 2 12

48的二进制值是“11 0000”,执行上述语句右移(右移2位)后返回值12,其二进制值是“00 1100”。