为什么Python将64位负数返回为非常大的正数?

问题描述 投票:0回答:1

问题来了。我预计结果是-100。我从其他 stackoverflow 问题中得知,python 使用 2 的补码来表示负数。如何将字符串转换为 64 位数字?

Windows下使用Python3。

Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> int('FFFFFFFFFFFFFF9C',16)
18446744073709551516
>>> int('0000000000000064',16)
100
python-3.x
1个回答
0
投票

你的假设是不正确的。 2 的补码意味着最高有效位决定数字的符号。 Python 的整数具有无限的范围,即无限的位数,因此没有最高有效位。

另请参阅

int.to_bytes(8)
将 Python 数字转换为 8 字节/64 位(如果可能 - 您可能会得到
OverflowError

© www.soinside.com 2019 - 2024. All rights reserved.