Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > binary number format ? format character %b or similar.

Reply
Thread Tools

binary number format ? format character %b or similar.

 
 
Ken Starks
Guest
Posts: n/a
 
      06-22-2008
I'm was wanting to format a positive integer in binary,
and not finding it--to my surprise--I rolled my own version.

Is this already in python, or have I missed it somewhere?

I have Googled around a bit, and found a few threads on
the subject, but they all seem to fizzle out.

(e.g. : INPUT 35, OUTPUT "100011" )
 
Reply With Quote
 
 
 
 
weheh
Guest
Posts: n/a
 
      06-22-2008
I don't know if you found this example:
http://www.daniweb.com/code/snippet285.html

--
> I'm was wanting to format a positive integer in binary,
> and not finding it--to my surprise--I rolled my own version.
>
> Is this already in python, or have I missed it somewhere?
>
> I have Googled around a bit, and found a few threads on
> the subject, but they all seem to fizzle out.
>
> (e.g. : INPUT 35, OUTPUT "100011" )



 
Reply With Quote
 
 
 
 
Ken Starks
Guest
Posts: n/a
 
      06-22-2008
weheh wrote:
> I don't know if you found this example:
> http://www.daniweb.com/code/snippet285.html
>


Thanks for that. The offerings are very similar to the
algorithms I wrote myself.

It wasn't the solution I was after,really; that's
easy. It was whether anything had found its way into
the standard library.
 
Reply With Quote
 
Mensanator
Guest
Posts: n/a
 
      06-23-2008
On Jun 22, 4:07�pm, Ken Starks <(E-Mail Removed)> wrote:
> weheh wrote:
> > I don't know if you found this example:
> >http://www.daniweb.com/code/snippet285.html

>
> Thanks for that. The offerings are very similar to the
> algorithms I wrote myself.
>
> It wasn't the solution I was after,really; that's
> easy. It was whether anything had found its way into
> the standard library.


Isn't that coming in Python 3.0?

You could also use gmpy, which has a lot of
other bit-functionality in addition to displaying
them.

>>> for i in xrange(:

print 'i:',gmpy.digits(i,2).zfill(,
print '# of 1-bits:',gmpy.popcount(i),
print 'find LS 1-bit:',gmpy.scan1(i),
print 'bit difference:',gmpy.hamdist(i,2**8-1)


i: 00000000 # of 1-bits: 0 find LS 1-bit: -1 bit difference: 8
i: 00000001 # of 1-bits: 1 find LS 1-bit: 0 bit difference: 7
i: 00000010 # of 1-bits: 1 find LS 1-bit: 1 bit difference: 7
i: 00000011 # of 1-bits: 2 find LS 1-bit: 0 bit difference: 6
i: 00000100 # of 1-bits: 1 find LS 1-bit: 2 bit difference: 7
i: 00000101 # of 1-bits: 2 find LS 1-bit: 0 bit difference: 6
i: 00000110 # of 1-bits: 2 find LS 1-bit: 1 bit difference: 6
i: 00000111 # of 1-bits: 3 find LS 1-bit: 0 bit difference: 5
 
Reply With Quote
 
Ken Starks
Guest
Posts: n/a
 
      06-23-2008
Mensanator wrote:
> On Jun 22, 4:07�pm, Ken Starks <(E-Mail Removed)> wrote:
>> weheh wrote:
>>> I don't know if you found this example:
>>> http://www.daniweb.com/code/snippet285.html

>> Thanks for that. The offerings are very similar to the
>> algorithms I wrote myself.
>>
>> It wasn't the solution I was after,really; that's
>> easy. It was whether anything had found its way into
>> the standard library.

>
> Isn't that coming in Python 3.0?


Thanks for the tip! following which, I found
information at:

http://docs.python.org/dev/3.0/libra...#formatstrings

>
> You could also use gmpy, which has a lot of
> other bit-functionality in addition to displaying
> them.
>


Yes, that'll be useful. Thanks again.
 
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
conversion of real number into binary number rajeswari01 Hardware 1 07-15-2011 05:30 AM
conversion of real number into binary number rajeswari01 Hardware 0 10-15-2010 12:30 AM
How do you convert a hexadecimal number to a binary number? Bob Sanders Ruby 5 05-30-2008 11:50 AM
converting exponential format number to decimal format number Fei Liu Perl Misc 21 12-16-2006 01:49 AM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM



Advertisments