Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45803
| Date | 2013-05-23 07:55 -0400 |
|---|---|
| From | Dave Angel <davea@davea.name> |
| Subject | Re: help in obtaining binary equivalent of a decimal number in python |
| References | <7f794f03-03c9-44de-8312-4ad33b68c266@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2014.1369310150.3114.python-list@python.org> (permalink) |
On 05/23/2013 07:30 AM, lokeshkoppaka@gmail.com wrote: > i need to get 32 bit binary equivalent of a decimal and need to change the 0's to 1's and 1's to 0's > For Example > if the input is 2 > Output should be: > the 32bit equivalent of 2 :0000 0000 0000 0000 0000 0000 0000 0010 > and the 1's compliment is:1111 1111 1111 1111 1111 1111 1111 1101 > > > > is there any pre-defined function to get the above results in python?? > I'm curious as to the intent of the assignment. Are you supposed to be learning about base conversion, about ones and twos complement, or about Python? Assuming the intent is to learn about Python, the built-in function bin() will take a Python integer (which is not decimal) and convert it to a str. At that point, you can manipulate the string any way you like. x = 45 print bin(45) 0b101101 Perhaps you want to start by stripping off the leading '0b' using a slice. Then you want to pad it to 32 columns by prepending some number of zeroes. Then you want to insert some spaces at regular intervals. Presumably doing the ones-complement operation on that string is then pretty easy for you. -- DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
help in obtaining binary equivalent of a decimal number in python lokeshkoppaka@gmail.com - 2013-05-23 04:30 -0700 Re: help in obtaining binary equivalent of a decimal number in python Chris Angelico <rosuav@gmail.com> - 2013-05-23 21:37 +1000 Re: help in obtaining binary equivalent of a decimal number in python Dave Angel <davea@davea.name> - 2013-05-23 07:55 -0400
csiph-web