473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

binary number format ? format character %b or similar.

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
4 1343
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Bern | last post by:
how to specifiy a binary number in c++? hex numbers are specified by 0x prefix
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
8
by: bearophileHUGS | last post by:
Hello, I have four things to ask or to suggest, sorry if they seem basic or already discussed. ------------------- I am still ignorant about Tkinter. This little program, after pressing the...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
26
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
3
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into...
3
by: zgfareed | last post by:
My program converts decimal numbers from to binary and hexadecimal. I am having trouble with my output which is supposed to be in a certain format. Binary needs to be in the format of XXXX XXXX...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.