The << and >> operators are bitshift operators. x << 1 shifts all the bits in x up to the next most significant bit, effectively multiplying by 2. More generally, x << n shifts the bits up n positions. To understand how this operation works it is easiest to look at the binary representation:
发布评论
评论(4)
<<
和>>
运算符是 位移运算符。 <代码>x << 1 将x
中的所有位向上移动到下一个最高有效位,有效地乘以 2。更一般地,x << n
将位向上移动 n 个位置。要了解此操作的工作原理,最简单的方法是查看 二进制 表示形式:类似地,
>>
运算符将位向下移动:The
<<
and>>
operators are bitshift operators.x << 1
shifts all the bits inx
up to the next most significant bit, effectively multiplying by 2. More generally,x << n
shifts the bits up n positions. To understand how this operation works it is easiest to look at the binary representation:Similarly the
>>
operator shifts the bits down:3 在二进制中为
11
,左移一位为110
,即十进制中的 6。想想
a << b
asa * (2 ** b)
>>
用于右移。想想a>> b
asa // (2 ** b)
3, in binary, is
11
and shifted to left one bit is110
, or 6 in decimal.Think of
a << b
asa * (2 ** b)
>>
is for right-shifting. Think ofa >> b
asa // (2 ** b)
这是一个轮班操作员。
http://docs.python.org/reference/expressions.html#shifting-operations
It's a shift operator.
http://docs.python.org/reference/expressions.html#shifting-operations
这是一个位移位,使用移位操作。
假设你有一个数字,查看最低位,你有 3:
0 0 1 1
如果你移动它,你会得到 6,或者:
0 1 1 0
It's a bit shift, using a shifting operation.
Say you have a number, and looking at the lowest bits, you have 3:
0 0 1 1
If you shift it, you'll get 6, or:
0 1 1 0