右移Right Shifts

带符号的整型负值右移位的结果The result of a right shift of a negative-value signed integral type

将负值向右移位可生成绝对值的一半(向下舍入)。Shifting a negative value to the right yields half the absolute value, rounded down. 例如,带符号 short 值 -253(十六进制 0xFF03,二进制 11111111 00000011)向右移一位会生成 -127(十六进制 0xFF81,二进制 11111111 10000001)。For example, a signed short value of -253 (hex 0xFF03, binary 11111111 00000011) shifted right one bit produces -127 (hex 0xFF81, binary 11111111 10000001). 正 253 向右移位生成 +126。A positive 253 shifted right produces +126.

右移保留带符号的整数类型的符号位。Right shifts preserve the sign bit of signed integral types. 当带符号的整数向右移位时,最高有效位将保留。When a signed integer shifts right, the most-significant bit remains set. 例如,如果 0xF0000000 是带符号的 int,则右移位生成 0xF8000000。For example, if 0xF0000000 is a signed int, a right shift produces 0xF8000000. 将负数 int 右移位 32 次会生成 0xFFFFFFFF。Shifting a negative int right 32 times produces 0xFFFFFFFF.

当无符号的整数右移位时,将清除最高有效位。When an unsigned integer shifts right, the most-significant bit is cleared. 例如,如果 0xF000 是无符号的,则结果为 0x7800。For example, if 0xF000 is unsigned, the result is 0x7800. unsigned 或正数 int 右移位 32 次会生成 0x00000000。Shifting an unsigned or positive int right 32 times produces 0x00000000.

另请参阅See Also

整数Integers