Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > integer to binary...

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
=?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
 
Reply With Quote
 
 
 
 
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://www.google.es/search?q=python+integer+to+binary

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
 
Reply With Quote
 
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...
 
Reply With Quote
 
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...


 
Reply With Quote
 
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
 
Reply With Quote
 
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!!
 
Reply With Quote
 
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...


 
Reply With Quote
 
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



 
Reply With Quote
 
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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I add an Integer to another Integer? Sebastian Stelzer Java 6 04-07-2010 07:03 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
how do I make Class.forName("Integer") returning java.lang.Integer? Johannes Zellner Java 22 12-19-2005 11:22 AM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments