473,609 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

howto print binary number

hi all,
could you inform how to print binary number?
I.e. something like

print '%b' % my_number

it would be nice would it print exactly 8 binary digits (0-1, with
possible start from 0)

Thank you in advance, D
Jun 27 '08 #1
5 18756
On May 7, 3:13*pm, dmitrey <dmitrey.kros.. .@scipy.orgwrot e:
hi all,
could you inform how to print binary number?
I.e. something like

print '%b' % my_number

it would be nice would it print exactly 8 binary digits (0-1, with
possible start from 0)

Thank you in advance, D
The gmpy works good.
>>import gmpy
print gmpy.digits(66, 2).zfill(8)
01000010
Jun 27 '08 #2
dmitrey wrote:
hi all,
could you inform how to print binary number?
I.e. something like

print '%b' % my_number

it would be nice would it print exactly 8 binary digits (0-1, with
possible start from 0)

Thank you in advance, D
--
http://mail.python.org/mailman/listinfo/python-list
Python 3.0 has such a formatting operation, but Python 2.x does not.
However it's not hard to write.

If you really only going to need 8 bits, you can just build a table of
bit patterns for each integer. Try this bit of slight cleverness:
>>b = ['']
for k in range(8):
.... b = [i+j for i in ['0','1'] for j in b]
....
>>len(b)
256
>>b[63]
'00111111'
Gary Herron

Jun 27 '08 #3
Python 3.0 has such a formatting operation, but Python 2.x does not.
However it's not hard to write.
Indeed. Refraining from using named lambdas:
>>def bin(x):
... return ''.join(x & (1 << i) and '1' or '0' for i in
... range(7,-1,-1))
...
>>bin(12)
'00001100'
>>bin(63)
'00111111'

It would be nice to have str(x, 2) like int(x, 2), but there are bigger
fish in pond.
Joel
Jun 27 '08 #4
On Wed, 07 May 2008 13:13:40 -0700, dmitrey wrote:
hi all,
could you inform how to print binary number? I.e. something like

print '%b' % my_number

it would be nice would it print exactly 8 binary digits (0-1, with
possible start from 0)

Thank you in advance, D
Here it is:

def bin(x, digits=0):
oct2bin = ['000','001','01 0','011','100', '101','110','11 1']
binstring = [oct2bin[int(n)] for n in oct(x)]
return ''.join(binstri ng).lstrip('0') .zfill(digits)
>>bin(5)
'101'
>>bin(0x0FFF, 16)
'00001111111111 11'
>>bin(5, 8)
'00000101'

--
Ivan
Jun 27 '08 #5
On May 7, 4:03*pm, Joel Bender <j...@cornell.e duwrote:
Python 3.0 has such a formatting operation, but Python 2.x does not. *
However it's not hard to write.

Indeed. *Refraining from using named lambdas:

* * *>>def bin(x):
* * *... * * return ''.join(x & (1 << i) and '1' or '0' for i in
* * *... * * * * range(7,-1,-1))
* * *...
* * *>>bin(12)
* * *'00001100'
* * *>>bin(63)
* * *'00111111'

It would be nice to have str(x, 2) like int(x, 2), but there are bigger
fish in pond.
+1 str( x, base ) +1
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2772
by: Rusty Shackleford | last post by:
I have a Summer in front of me without any school, and I'd like to add a new format for python print strings that will show any number in a binary representation. For example: >>> '%b' % 3 11 >>> '%b' % 5 101 You get the idea. I've written functions that return strings, so that
12
16092
by: neutrino | last post by:
Greetings to the Python gurus, I have a binary file and wish to see the "raw" content of it. So I open it in binary mode, and read one byte at a time to a variable, which will be of the string type. Now the problem is how to print the binary format of that charater to the standard output. It seems a common task but I just cannot find the appropriate method from the documentation. Thanks a lot.
11
5852
by: CSN | last post by:
Is it possible to iterate over an array in plpgsql? Something like: function insert_stuff (rel_ids int) .... foreach rel_ids as id insert into table (rel_id, val) values (id, 5);
3
1427
by: durumdara | last post by:
Hi! I want to replace some seqs. in a html. Let: a- b = ab but: xxx -
6
10554
by: fnoppie | last post by:
Hi, I am near to desperation as I have a million things to get a solution for my problem. I have to post a multipart message to a url that consists of a xml file and an binary file (pdf). Seperately the posting words fine but when I want to create one multipart message with both then things go wrong. The binary file is converted and of datatype byte() The xml file is just a string.
14
2312
by: mosi | last post by:
Problem: how to get binary from integer and vice versa? The simplest way I know is: a = 0100 a 64 but: a = 100 (I want binary number) does not work that way.
12
11494
by: waterdriven | last post by:
Hello; I am a newbie. A homework assignment was assigned and I am having trouble getting started. The assignment reads: Write a program to print out the binary value of a 16 bit number.
0
1272
by: castironpi | last post by:
On May 7, 3:31 pm, Mensanator <mensana...@aol.comwrote: ) for a in range( 10 ) ] ) 00000000 00000001 00000010 00000011 00000100 00000101 00000110
45
3777
by: hank | last post by:
I have this code here, it converts decimal numbers to binary. There is one problem. The output is printed 'in reverse' and I have no clue at all how to solve this problem. Output of program: Decimal number: 10 Binary representation: 0101 (It should be 1010, and not 0101...)
0
8065
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8563
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6990
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5507
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2527
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.