gmpy / binary()

Duncan Smith buzzard at urubu.freeserve.co.uk
Tue Dec 18 17:48:39 CET 2001


My code:

bit_stuff.py
----------------------------------------------
import gmpy

def combinations(n, k):
    comb = gmpy.mpz(1)
    for i in range(k):
        comb = comb.setbit(i)      #first combination
    print comb, comb.binary()
    max = comb << (n-k)          #last combination
    print max, max.binary()
------------------------------------------------------

>>> reload(bit_stuff)
<module 'bit_stuff' from 'C:\Python20\anygui-0.1a3\bit_stuff.py'>
>>> bit_stuff.combinations(13,6)
63 ?
8064 ?    #actually a ? followed by a shaded box with the letters US
>>> bit_stuff.combinations(14,6)
63 ?
16128
>>> x = gmpy.mpz(8064)
>>> x.binary()
'\200\037'
>>> y = gmpy.mpz(16128)
>>> y.binary()
'\000?'
>>> z = gmpy.mpz(63)
>>> z.binary()
'?'

Why the different output? eg. ? versus '\200\037' and nothing versus
'\000?'.  Thanks in advance.

Duncan





More information about the Python-list mailing list