Velocity Reviews > integer to binary...

# integer to binary...

nicolasg@gmail.com
Guest
Posts: n/a

 06-01-2006
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?=
Guest
Posts: n/a

 06-01-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) schrieb:
> does anyone know a module or something to convert numbers like integer
> to binary format ?

unfortunately there is no builtin function for this

>>> int("111",2)

7
>>> str(7)

'7'
>>> str(7,2)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: str() takes at most 1 argument (2 given)
>>>

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>

</rebel on>

> 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

Alexis Roda
Guest
Posts: n/a

 06-01-2006
En/na (E-Mail Removed) ha escrit:
> does anyone know a module or something to convert numbers like integer
> to binary format ?

http://aspn.activestate.com/ASPN/Coo.../Recipe/219300

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

python already provides some bitwise operators:

http://docs.python.org/ref/summary.html

HTH

Grant Edwards
Guest
Posts: n/a

 06-01-2006
On 2006-06-01, (E-Mail Removed) <(E-Mail Removed)> wrote:

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

>>> 7 & 3

3
>>> 7 | 8

15

--
Grant Edwards grante Yow! QUIET!! I'm being
at CREATIVE!! Is it GREAT
visi.com yet? It's s'posed to SMOKEY
THE BEAR...

nicolasg@gmail.com
Guest
Posts: n/a

 06-01-2006

Grant Edwards wrote:
> On 2006-06-01, (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> > 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:
>
> >>> 7 & 3

> 3
> >>> 7 | 8

> 15
>
>
> --

I know I can do that but I need to operate in every bit separeted.
> Grant Edwards grante Yow! QUIET!! I'm being
> at CREATIVE!! Is it GREAT
> visi.com yet? It's s'posed to SMOKEY
> THE BEAR...

Anton Vredegoor
Guest
Posts: n/a

 06-01-2006
(E-Mail Removed) 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...

>>> def bits(i,n):

return tuple((0,1)[i>>j & 1] for j in xrange(n-1,-1,-1))

>>> bits(7,4)

(0, 1, 1, 1)

Anton

Grant Edwards
Guest
Posts: n/a

 06-01-2006
On 2006-06-01, (E-Mail Removed) <(E-Mail Removed)> wrote:

>>> 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:
>>
>> >>> 7 & 3

>> 3
>> >>> 7 | 8

>> 15

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

Sorry, I've no clue what that means.

--
Grant Edwards grante Yow! Now KEN is having
at a MENTAL CRISIS beacuse
visi.com his "R.V." PAYMENTS are
OVER-DUE!!

nicolasg@gmail.com
Guest
Posts: n/a

 06-01-2006

(E-Mail Removed) wrote:
> Grant Edwards wrote:
> > On 2006-06-01, (E-Mail Removed) <(E-Mail Removed)> wrote:
> >
> > > 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:
> >
> > >>> 7 & 3

> > 3
> > >>> 7 | 8

> > 15
> >
> >

this is exactly what I need ->
http://www.daniweb.com/code/snippet285.html

thanks.
> > --

> I know I can do that but I need to operate in every bit separeted.
> > Grant Edwards grante Yow! QUIET!! I'm being
> > at CREATIVE!! Is it GREAT
> > visi.com yet? It's s'posed to SMOKEY
> > THE BEAR...

Tim Chase
Guest
Posts: n/a

 06-01-2006
>>> for example I want to convert number 7 to 0111 so I can make some
>>> bitwise operations...

>> Just do it:
>>
>>>>> 7 & 3

>> 3
>>>>> 7 | 8

>> 15

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

>>> target = 10
>>> shift = 0
>>> while 1 << shift <= target:

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

Grant Edwards
Guest
Posts: n/a

 06-01-2006
On 2006-06-01, (E-Mail Removed) <(E-Mail Removed)> wrote:

>>>> 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:
>>>
>>> >>> 7 & 3
>>> 3
>>> >>> 7 | 8
>>> 15

>>

> this is exactly what I need -> http://www.daniweb.com/code/snippet285.html

That's nice, but I don't register at web sites like that.

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

I still don't get what you want a binary string for.

I can see wanting a sequence (e.g. array) of boolean values,
but how are you going to do bitwise operations on a binary
string?

--
Grant Edwards grante Yow! .. I think I'd
at better go back to my DESK
visi.com and toy with a few common
MISAPPREHENSIONS...