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?

2
  • What do you mean by "a bitboard"? How many bits do you really need? What's special about 64-bit integers that would help you solve your problem? What exactly are you trying to do? Commented Dec 30, 2011 at 6:48
  • 2
    If you want to do bit-twiddling, Python is a very poor choice as the operations will not be compiled into a small number of CPU arithmetic instructions. Commented Dec 30, 2011 at 9:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Does this answer your question?
Basically I need 64 bits to represent a chess board. I have been reading the source code of some programs and they use mpz. But unfortunately it's not documented very well. If I want to store an integer in 64 bits what should I do? Could you give an example?
@Mark You are looking at some rather outdated code. From PEP 4: The mpz module has been documented as obsolete since Python 2.2. Removed from the library reference in Python 2.4.
@JanneKarila: I think MPZ was reimplemented in a module called gmpy. Most programs I am reading seems to include mpz from gmpy.
@Mark OK, then see gmpy docs: An mpz object can be transformed into a Python number by passing it as the argument of a call to Python's built-in number types (int, long, float, complex). For example, use long(x)
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.

3 Comments

Are bitarrays as fast as mpz?
I haven't compared the performance with bitarray. In the example above, xmpz bit access is about twice as fast as mpz.
With gmpy after a=a.setbit(48) the value of a changes to mpz(281474976710663) because of the first bit. I'm not sure why is this the case, is this the meant behaviour? If I manually change the beginning of the '0b0' instead of '0b1' (as it weas introduced by the conversion to 48 bits) it works fine. Is there a better way around it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.