带无符号值的位移位给出带符号的结果
Bitshift with unsigned value gives signed result
对无符号 uint32
变量进行位移运算,为什么收到有符号 int
结果?
func NewNM(log2Dim uint32) {
SIZE := 1 << (3 * log2Dim) // Why: SIZE type == int
// ...
}
The right operand in a shift expression must have integer type or be
an untyped constant representable by a value of type uint. If the left
operand of a non-constant shift expression is an untyped constant, it
is first implicitly converted to the type it would assume if the shift
expression were replaced by its left operand alone.
以后...
The shift operators shift the left operand by the shift count
specified by the right operand, which must be non-negative. If the
shift count is negative at run time, a run-time panic occurs. The
shift operators implement arithmetic shifts if the left operand is a
signed integer and logical shifts if it is an unsigned integer.
所以表达式1 << (3 * log2Dim)
中的1
根据第一段转换为一个int
,然后移位是根据第二段的算术移位。
对无符号 uint32
变量进行位移运算,为什么收到有符号 int
结果?
func NewNM(log2Dim uint32) {
SIZE := 1 << (3 * log2Dim) // Why: SIZE type == int
// ...
}
The right operand in a shift expression must have integer type or be an untyped constant representable by a value of type uint. If the left operand of a non-constant shift expression is an untyped constant, it is first implicitly converted to the type it would assume if the shift expression were replaced by its left operand alone.
以后...
The shift operators shift the left operand by the shift count specified by the right operand, which must be non-negative. If the shift count is negative at run time, a run-time panic occurs. The shift operators implement arithmetic shifts if the left operand is a signed integer and logical shifts if it is an unsigned integer.
所以表达式1 << (3 * log2Dim)
中的1
根据第一段转换为一个int
,然后移位是根据第二段的算术移位。