2017-04-19 70 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!

相关问题