带有故意溢出的python 32位和64位整数数学 [英] python 32-bit and 64-bit integer math with intentional overflow
本文介绍了带有故意溢出的python 32位和64位整数数学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在32位和64位进行整数数学运算的最佳方法是什么,以便像C语言一样发生溢出?
What's the best way to do integer math in 32- and 64-bit, so that overflow happens like it does in C?
例如(65536 * 65536 + 1)*(65536 * 65536 + 1)在64位数学运算中应为0x0000000200000001,而不是其确切值(非溢出)0x10000000200000001.
e.g. (65536*65536+1)*(65536*65536+1) should be 0x0000000200000001 in 64-bit math, and not its exact value (non-overflowing) 0x10000000200000001.
推荐答案
仅&
使用适当的32位或64位掩码(0xffffffff
或0xffffffffffffffff
)的结果.
Just &
the result with the appropriate 32- or 64-bit mask (0xffffffff
or 0xffffffffffffffff
).
这篇关于带有故意溢出的python 32位和64位整数数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文