>>> a = -2147458560
>>> bin(a)
'-0b1111111111111111001111000000000'
我的意图是将a
作为32位有符号二进制文件进行操作并返回它。 -2147458560
的正确转换为'0b10000000000000000110001000000000'
;我怎么能实现呢?
答案 0 :(得分:14)
&
(2 32 - 1) 按位AND (0xffffffff
):
>>> a = -2147458560
>>> bin(a & 0xffffffff)
'0b10000000000000000110001000000000'
>>> format(a & 0xffffffff, '32b')
'10000000000000000110001000000000'
>>> '{:32b}'.format(a & 0xffffffff)
'10000000000000000110001000000000'