integer to binary...
Claudio Grondi
claudio.grondi at freenet.de
Fri Jun 2 10:08:25 EDT 2006
mensanator at aol.com wrote:
> nicolasg at gmail.com wrote:
>
>>does anyone know a module or something to convert numbers like integer
>>to binary format ?
>>
>>for example I want to convert number 7 to 0111 so I can make some
>>bitwise operations...
>>
>>Thanks
>
>
> Use the gmpy module.
>
>
>>>>import gmpy
>>>>a = 14
>>>>b = 7
>>>>c = 8
>
>
>>>>help(gmpy.digits)
>
> Help on built-in function digits:
>
> digits(...)
> digits(x[,base]): returns Python string representing x in the
> given base (2 to 36, default 10 if omitted or 0); leading '-'
> present if x<0, but no leading '+' if x>=0. x must be an mpz,
> or else gets coerced into one.
>
>
>>>>print gmpy.digits(a,2)
>
> 1110
>
>>>>print gmpy.digits(b,2)
>
> 111
>
>>>>print gmpy.digits(c,2)
>
> 1000
>
>
>
>>>>help(gmpy.setbit)
>
> Help on built-in function setbit:
>
> setbit(...)
> setbit(x,n,v=1): returns a copy of the value of x, with bit n set
> to value v; n must be an ordinary Python int, >=0; v, 0 or !=0;
> x must be an mpz, or else gets coerced to one.
>
>
>>>>d = gmpy.setbit(c,1,1)
>>>>print gmpy.digits(d,2)
>
> 1010
>
>
>
>
>>>>help(gmpy.scan1)
>
> Help on built-in function scan1:
>
> scan1(...)
> scan1(x, n=0): returns the bit-index of the first 1-bit of x (that
> is at least n); n must be an ordinary Python int, >=0. If no more
> 1-bits are in x at or above bit-index n (which can only happen for
> x>=0, notionally extended with infinite 0-bits), None is returned.
> x must be an mpz, or else gets coerced to one.
>
>
>>>>help(gmpy.scan0)
>
> Help on built-in function scan0:
>
> scan0(...)
> scan0(x, n=0): returns the bit-index of the first 0-bit of x (that
> is at least n); n must be an ordinary Python int, >=0. If no more
> 0-bits are in x at or above bit-index n (which can only happen for
> x<0, notionally extended with infinite 1-bits), None is returned.
> x must be an mpz, or else gets coerced to one.
>
>
>>>>print gmpy.scan1(a)
>
> 1
>
>>>>print gmpy.scan1(b)
>
> 0
>
>>>>print gmpy.scan1(c)
>
> 3
>
>>>>print gmpy.scan1(d)
>
> 1
>
>>>>print gmpy.scan0(a)
>
> 0
>
>>>>print gmpy.scan0(b)
>
> 3
>
>>>>print gmpy.scan0(c)
>
> 0
>
>>>>print gmpy.scan0(d)
>
> 0
>
>
>>>>help(gmpy.popcount)
>
> Help on built-in function popcount:
>
> popcount(...)
> popcount(x): returns the number of 1-bits set in x; note that
> this is 'infinite' if x<0, and in that case, -1 is returned.
> x must be an mpz, or else gets coerced to one.
>
>
>>>>print gmpy.popcount(a)
>
> 3
>
>>>>print gmpy.popcount(b)
>
> 3
>
>>>>print gmpy.popcount(c)
>
> 1
>
>>>>print gmpy.popcount(d)
>
> 2
>
>
>
>>>>help(gmpy.hamdist)
>
> Help on built-in function hamdist:
>
> hamdist(...)
> hamdist(x,y): returns the Hamming distance (number of bit-positions
> where the bits differ) between x and y. x and y must be mpz, or
> else
> get coerced to mpz.
>
>
>>>>print gmpy.hamdist(a,b)
>
> 2
>
>>>>print gmpy.hamdist(a,c)
>
> 2
>
>>>>print gmpy.hamdist(a,d)
>
> 1
>
>>>>print gmpy.hamdist(b,c)
>
> 4
>
>>>>print gmpy.hamdist(b,d)
>
> 3
>
>>>>print gmpy.hamdist(c,d)
>
> 1
>
For those digging deeper into this subject who are looking for speed,
reading the past discussion on this newsgroup I was part of myself
looking for fastest way of such integer to binary conversion can maybe
be of interest:
http://mail.python.org/pipermail/python-list/2006-January/319295.html
(includes full source code of all compared approaches)
Claudio
More information about the Python-list
mailing list