Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45798 > unrolled thread
| Started by | lokeshkoppaka@gmail.com |
|---|---|
| First post | 2013-05-23 04:30 -0700 |
| Last post | 2013-05-23 07:55 -0400 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | lokeshkoppaka@gmail.com |
|---|---|
| Date | 2013-05-23 04:30 -0700 |
| Subject | help in obtaining binary equivalent of a decimal number in python |
| Message-ID | <7f794f03-03c9-44de-8312-4ad33b68c266@googlegroups.com> |
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??
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-05-23 21:37 +1000 |
| Message-ID | <mailman.2012.1369309056.3114.python-list@python.org> |
| In reply to | #45798 |
On Thu, May 23, 2013 at 9:30 PM, <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?? You're asking for bitwise negation. Now that you know the keyword(s) to look for, you should be able to figure out the rest with a few quick docs and/or web searches. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-05-23 07:55 -0400 |
| Message-ID | <mailman.2014.1369310150.3114.python-list@python.org> |
| In reply to | #45798 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web