5.7 移位运算符 Shifting operations

The shifting operations have lower priority than the arithmetic operations:

移位运算符的优先级比算术运算符低.

shift_expr  ::=  a_expr | shift_expr ( "«" | "»" ) a_expr
Download entire grammar as text.

These operators accept plain or long integers as arguments. The arguments are converted to a common type. They shift the first argument to the left or right by the number of bits given by the second argument.

这些运算符接受普通整数和长整数作为参数. 参数都被转换通用类型.它们将第一个参数向左或向右移动第二个参数指出的位数.

A right shift by n bits is defined as division by pow(2,n). A left shift by n bits is defined as multiplication with pow(2,n); for plain integers there is no overflow check so in that case the operation drops bits and flips the sign if the result is not less than pow(2,31) in absolute value. Negative shift counts raise a ValueError exception.

右移n位可以定义为除以pow(2,n),左移n位可以定义为乘以pow(2,n); 对于普通整数是没有溢出检查的,因此若结果的绝对值不小于pow(2,31), 这个运算会截掉相应的位并且符号位也在移位处理之列.