gpt4 book ai didi

python - 负整数的位移位?

转载 作者:行者123 更新时间:2023-11-28 16:21:59 26 4
gpt4 key购买 nike

我想了解负整数的位移位操作 >>>

-2 >> 1 # -1
-3 >> 1 # -2
-5 >> 1 # -3
-7 >> 1 # -4

谁能解释一下这是怎么做到的?我知道它与 Two 的补码有关,但我无法将其与移位运算相关联。

最佳答案

提供了完整的解释here

Two's Complement binary for Negative Integers:

Negative numbers are written with a leading one instead of a leading zero. So if you are using only 8 bits for your twos-complement numbers, then you treat patterns from "00000000" to "01111111" as the whole numbers from 0 to 127, and reserve "1xxxxxxx" for writing negative numbers. A negative number, -x, is written using the bit pattern for (x-1) with all of the bits complemented (switched from 1 to 0 or 0 to 1). So -1 is complement(1 - 1) = complement(0) = "11111111", and -10 is complement(10 - 1) = complement(9) = complement("00001001") = "11110110". This means that negative numbers go all the way down to -128 ("10000000").

Of course, Python doesn't use 8-bit numbers. It USED to use however many bits were native to your machine, but since that was non-portable, it has recently switched to using an INFINITE number of bits. Thus the number -5 is treated by bitwise operators as if it were written "...1111111111111111111011".

那么,移位运算符的解释:

x >> y Returns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y.

为了理解上面的解释,你可以用这样的方式来检查:

def twos_comp(val, nbits):
"""Compute the 2's complement of int value val"""
if val < 0:
val = (1 << nbits) + val
else:
if (val & (1 << (nbits - 1))) != 0:
# If sign bit is set.
# compute negative value.
val = val - (1 << nbits)
return val

def foo(a,b):
print("{0:b} >> {1:b} = {2:b} <==> {3:b} >> {4:b} = {5:b}".format(
a,b,a>>b,
twos_comp(a,8),b, twos_comp(a>>b,8)
))

foo(-2, 1)
foo(-3, 1)
foo(-5, 1)
foo(-7, 1)

哪些输出:

-10 >> 1 = -1 <==> 11111110 >> 1 = 11111111
-11 >> 1 = -10 <==> 11111101 >> 1 = 11111110
-101 >> 1 = -11 <==> 11111011 >> 1 = 11111101
-111 >> 1 = -100 <==> 11111001 >> 1 = 11111100

如您所见,数字的补码将扩展符号。

关于python - 负整数的位移位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39549971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com