2017-04-19 52 views
0

誰能解釋這Python代碼如何:左移一個Python整數產生不正確的結果

print "bits_received:", bits_received 
tmp = 0 
print type(tmp) 
tmp = (1 << bits_received) 
print type(tmp) 
print "tmp: 0x{:X}".format(tmp) 

會產生這樣的結果:

bits_received: 95 
<type 'int'> 
<type 'numpy.int64'> 
tmp: 0x80000000 
+2

'bits_received'是一個NumPy標量。 NumPy整數是固定寬度的,就像C一樣。NumPy好像給了你底層C編譯器的左移行爲(這可能是底層機器的左移行爲),而不是引發錯誤。 – user2357112

回答

1

user2357112是正確的:

bits_received: 88 <type 'int'> 
bits_received: 95 <type 'numpy.int64'> 

bits_received變量正從它的類型變爲intnumpy.int64,通過添加另一種類型的變量numpy.int64。用「int(...)」包裝其他變量解決了我的問題。

謝謝,user2357112!

相關問題