Alex Rivera | Logout

getting 64 bit integer in python

Asked 2011-12-30T06:05:55.800
15

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?

Edit
Report

2 Answers

41

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.

answered 2011-12-30T06:23:25.897
3

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.

answered 2011-12-30T07:41:31.237

Your Answer