By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,666 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,666 IT Pros & Developers. It's quick & easy.

binary number format ? format character %b or similar.

P: n/a
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" )
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
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" )

Jun 27 '08 #2

P: n/a
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.
Jun 27 '08 #3

P: n/a
On Jun 22, 4:07�pm, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
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(8):
print 'i:',gmpy.digits(i,2).zfill(8),
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
Jun 27 '08 #4

P: n/a
Mensanator wrote:
On Jun 22, 4:07�pm, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
>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.
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.