在python中获取64位整数

发布于 2024-12-23 08:09:58 字数 114 浏览 2 评论 0原文

所以我正在考虑用 python 或 lisp 编写一个位板。但我不知道如何确保在 python 中获得 64 位整数。我一直在阅读文档,发现 mpz 库返回一个无符号 32 位整数。这是真的吗?如果不是我该怎么办?

So I am thinking of writing a bitboard in python or lisp. But I don't know how to ensure I would get a 64 bit integer in python. I have been reading documentation and found that mpz library returns a unsigned 32 bit integer. Is this true? If not what should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜柠檬 2024-12-30 08:09:58

Python 2 有两种整数类型:int,它是一个有符号整数,其大小等于机器的字大小(但始终至少为 32 位),以及 long,它是大小不受限制。

Python 3 只有一种整数类型,称为 int,但相当于 Python 2 long

Python 2 has two integer types: int, which is a signed integer whose size equals your machine's word size (but is always at least 32 bits), and long, which is unlimited in size.

Python 3 has only one integer type, which is called int but is equivalent to a Python 2 long.

2024-12-30 08:09:58

使用 gmpy 时,您有多种选择。下面是一个使用 gmpy 的示例:

>>> from gmpy import mpz
>>> a=mpz(7)
>>> bin(a)
'0b111'
>>> a=a.setbit(48)
>>> bin(a)
'0b1000000000000000000000000000000000000000000000111'
>>> 

gmpy2 是 gmpy 的开发版本,包含一种名为 xmpz 的新类型,它允许更直接地访问位。

>>> from gmpy2 import xmpz
>>> a=xmpz(7)
>>> bin(a)
'0b111'
>>> a[48]=1
>>> bin(a)
'0b1000000000000000000000000000000000000000000000111'
>>> 

您可能还想查看其他解决方案,例如 bitarray

免责声明:我维护 gmpy 和 gmpy2。

You have a couple of options using gmpy. Here is one example using gmpy:

>>> from gmpy import mpz
>>> a=mpz(7)
>>> bin(a)
'0b111'
>>> a=a.setbit(48)
>>> bin(a)
'0b1000000000000000000000000000000000000000000000111'
>>> 

gmpy2 is the development version of gmpy and includes a new type called xmpz that allows more direct access to the bits.

>>> from gmpy2 import xmpz
>>> a=xmpz(7)
>>> bin(a)
'0b111'
>>> a[48]=1
>>> bin(a)
'0b1000000000000000000000000000000000000000000000111'
>>> 

There are other solutions such as bitarray you might want to look at.

Disclaimer: I maintain gmpy and gmpy2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文