473,387 Members | 1,650 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,387 software developers and data experts.

Binary numbers

Hi !

How to work with binary numbers in python ? Is there a way to print a number
in its binary form like we do with oct() or hex() ?

Im doing a project that i have to work with binaries and i tired of convert
numbers to string all the time to perform some operations.

I searched about it in many places like python.org and google, but not found
anything useful.

Thats why im asking this.

And another question... if python has not a way to do this, why i let me use
oct(), hex() and not bin() ?

Thanks for the help !

--
Douglas Soares de Andrade
http://douglasandrade.cjb.net - dsa at unilestemg.br
UnilesteMG - www.unilestemg.br
ICQ, MSN = 76277921, douglas at tuxfamily.org

Jul 19 '05 #1
6 5068
Douglas Soares de Andrade wrote:
Hi !

How to work with binary numbers in python ? Is there a way to print a number
in its binary form like we do with oct() or hex() ?

Im doing a project that i have to work with binaries and i tired of convert
numbers to string all the time to perform some operations.

I searched about it in many places like python.org and google, but not found
anything useful.


Search for "itoa".

Jul 19 '05 #2
Hi !

Pardon me, but what itoa has to do it the history ?

See ya !

Em Quarta 08 Junho 2005 00:34, Dan Bishop escreveu:
Douglas Soares de Andrade wrote:
Hi !

How to work with binary numbers in python ? Is there a way to print a
number in its binary form like we do with oct() or hex() ?

Im doing a project that i have to work with binaries and i tired of
convert numbers to string all the time to perform some operations.

I searched about it in many places like python.org and google, but not
found anything useful.


Search for "itoa".


--
Douglas Soares de Andrade
http://douglasandrade.cjb.net - dsa at unilestemg.br
UnilesteMG - www.unilestemg.br
ICQ, MSN = 76277921, douglas at tuxfamily.org

Jul 19 '05 #3
Douglas Soares de Andrade wrote:
Hi !

How to work with binary numbers in python ? Is there a way to print a number
in its binary form like we do with oct() or hex() ?


This is a popular topic in the Python Cookbook, maybe one of these recipes will suit you:
http://aspn.activestate.com/ASPN/Coo.../Recipe/219300
http://aspn.activestate.com/ASPN/Coo.../Recipe/111286
http://aspn.activestate.com/ASPN/Coo...n/Recipe/65212

Kent
Jul 19 '05 #4


Douglas Soares de Andrade wrote:
Hi !

How to work with binary numbers in python ? Is there a way to print a number
in its binary form like we do with oct() or hex() ?

Im doing a project that i have to work with binaries and i tired of convert
numbers to string all the time to perform some operations.

I searched about it in many places like python.org and google, but not found
anything useful.

Thats why im asking this.

And another question... if python has not a way to do this, why i let me use
oct(), hex() and not bin() ?

Thanks for the help !

--
Douglas Soares de Andrade
http://douglasandrade.cjb.net - dsa at unilestemg.br
UnilesteMG - www.unilestemg.br
ICQ, MSN = 76277921, douglas at tuxfamily.org


You might want to try the GMPY module:
from gmpy import *
help(digits) Help on built-in function digits:

digits(...)
digits(x[,base]): returns Python string representing x in the
given base (2 to 36, default 10 if omitted or 0); leading '-'
present if x<0, but no leading '+' if x>=0. x must be an mpz,
or else gets coerced into one.
x = 2**32 - 1
print x 4294967295 print digits(x,2)

11111111111111111111111111111111

Jul 19 '05 #5
Don't know if this is what you mean, but:

Binary to decimal:
bin_num = '100001011'
int(bin_num, 2) 267
def dec2bin(dec_number): .... if dec_number == 0: return '0'
.... return (dec2bin(dec_number >> 1) + str(dec_number % 2))
.... dec2bin(267)

'0100001011'

--
Guyon Morée
gu*************@Mgmail.com
http://gumuz.looze.net

Jul 19 '05 #6
Hi!

Em Quarta 08 Junho 2005 09:38, Guyon Morée escreveu:
Don't know if this is what you mean, but:

Binary to decimal:
bin_num = '100001011'
int(bin_num, 2)


267


Dont know this way of using it. Thanks for the teachings :)

See ya !

--
Douglas Soares de Andrade
http://douglasandrade.cjb.net - dsa at unilestemg.br
UnilesteMG - www.unilestemg.br
ICQ, MSN = 76277921, douglas at tuxfamily.org

Jul 19 '05 #7

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

Similar topics

9
by: J.Sperlhofer | last post by:
Good morning, Javascript-Professionals. I'm looking for an possibility to show a (calculated) 64bit-Number without exponential notation. I don't want to see exponational notation within my...
6
by: Kelvin | last post by:
Hi everyone: when we wanna use hex numbers in C, we usually write something like: int hex_num = 0x12F9; but how can I declare a binary number in a similar way by putting some leading words to...
1
by: Seth King | last post by:
I am used to programming in embedded C and I want to input a binary number into an int. usually I would just use bin as a suffix or b as a prefix int x = b001 or int x = 001110bin the hex...
5
by: Bint | last post by:
Hi, I have an array of numbers, and I need to turn it into a single binary string. How do I do that? Thanks B
10
by: Craig | last post by:
Hi there, I'm trying to switch binary numbers around so that the MSB becomes the LSB etc. Is there an easy way of doing this as I can't seem to find anything. If you could help that would be...
8
Asghar baig
by: Asghar baig | last post by:
Computer understands information only in binary format i.e. in the form of zeros and ones. Do you think that Binary number system is essential for computers to understand and process the information...
2
by: shalombizal | last post by:
I am a student of Electrical Engineering I want to write a C++ program to realize arthimetic operation in binary. can any body help me by giving C++ code for signed mulitplication and Division of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.