integer to binary...

N

nicolasg

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
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

does anyone know a module or something to convert numbers like integer
to binary format ?

unfortunately there is no builtin function for this
Traceback (most recent call last):

int, str are not symmetrical
I hope this will change in future

<rebel on>

you can use Ruby's 7.to_s(2) for this
irb(main):001:0> 7.to_s(2)
=> "111"
irb(main):002:0> 7.to_s(3)
=> "21"
irb(main):003:0>

for example I want to convert number 7 to 0111 so I can make some
bitwise operations...

you can use bitwise operations on int's anyway

7 & 3 == 3
(1 << 20) | (1 << 10) == 2**20+2**10

and so on
 
G

Grant Edwards

does anyone know a module or something to convert numbers like integer
to binary format ?

They _are_ in binary format.
for example I want to convert number 7 to 0111 so I can make some
bitwise operations...

Just do it:
15
 
A

Anton Vredegoor

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...
return tuple((0,1)[i>>j & 1] for j in xrange(n-1,-1,-1))
(0, 1, 1, 1)

Anton
 
T

Tim Chase

for example I want to convert number 7 to 0111 so I can make some
I know I can do that but I need to operate in every bit separeted.


I suppose there might be other operations for which having them
as strings could be handy. E.g. counting bits:

bitCount = len([c for c in "01001010101" if c=="1"])

or parity checking with those counted bits...sure, it can be done
with the raw stuff, but the operations often tend to be more obscure.

Other reasons for wanting an arbitrary integer in binary might be
for plain-old-display, especially if it represents bitmap data.

If you just want to operate on each bit, you can iterate over the
number of bits and shift a single bit to its position:
.... print "Bit %i is %i" % (shift,
.... (target & (1 << shift)) >> shift)
.... shift += 1
....
Bit 0 is 0
Bit 1 is 1
Bit 2 is 0
Bit 3 is 1


It's ugly, but it works...

-tkc
 
G

Grant Edwards

I know I can do that but I need to operate in every bit separeted.


I suppose there might be other operations for which having them
as strings could be handy. E.g. counting bits:

bitCount = len([c for c in "01001010101" if c=="1"])

or parity checking with those counted bits...sure, it can be done
with the raw stuff, but the operations often tend to be more obscure.

I would think an array or list of bits would be a lot more
useful for doing "bitwise operations":

bitCount = sum([0,1,0,0,1,0,1,0,1,0,1])
parity = reduce(operator.xor,[0,1,0,0,1,0,1,0,1,0,1])
Other reasons for wanting an arbitrary integer in binary might be
for plain-old-display, especially if it represents bitmap data.

Yes. I thought C should have had a %b format since the
beginning, but nobody listens. But that's not
what the OP said he wanted it for.
 
M

mensanator

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.
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 '-'
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.
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 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.
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.
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.
1
 
C

Claudio Grondi

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.


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.

1110

111

1000

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.


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 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.

1

0

3

1

0

3

0

0

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.


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.

2

2

1

4

3

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
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
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...

You don't need to convert anything. The bitwise ops are &, |, <<, >>

0 | 2 | 4
-> 6
6 & 2
-> 2
2 << 4
-> 32
8 >> 1
-> 4
 
B

Bruno Desthuilliers

Grant Edwards a écrit :
They _are_ in binary format.

Not really.
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__',
'__hex__', '__init__', '__int__', '__invert__', '__long__',
'__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
'__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
'__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',
'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
'__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']
 
G

Grant Edwards

Grant Edwards a écrit :

Not really.

Yes, really. Otherwise the bitwise boolean operations you
demonstrated wouldn't work as shown.
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__',
'__hex__', '__init__', '__int__', '__invert__', '__long__',
'__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
'__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
'__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',
'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
'__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']

The fact that they impliment the xor operator is pretty much
proof that integers are stored in binary format -- xor is only
defined for binary numbers.
 
T

Tim Chase

The fact that they impliment the xor operator is pretty much
proof that integers are stored in binary format -- xor is only
defined for binary numbers.

Um...let's not use bad logic/proofs for evidencing this...
True

:)

-tkc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top