Bitwise xor 0xFFFFFFFF?

后端 未结 2 1799
忘了有多久
忘了有多久 2021-01-25 03:54

I couldn\'t wrap my head around this:

def expr(a):
    return ~(a ^ 0xFFFFFFFF), a ^ 0xFFFFFFFF, ~a, a

print(expr(0xFFFFFFFF))
print(expr(1))
print(expr(0))  
p         


        
2条回答
  •  没有蜡笔的小新
    2021-01-25 04:36

    0xFFFFFFFF is a large number; it's the hexadecimal representation of 232-1. Python's ints are represented internally as a linked list of C longs, allowing theoretically unbounded size. Bitwise XOR (^) in Python uses zero values for the bits more significant than those you've given, so the net result is that only the lower 32 bits are flipped, resulting in behavior that diverges from what you get in C, where there are only 32 bits and the net result is that 'all' the bits are flipped.

提交回复
热议问题