Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45803

Re: help in obtaining binary equivalent of a decimal number in python

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'insert': 0.05; 'output': 0.05; 'binary': 0.07; 'subject:help': 0.08; 'string': 0.09; 'assuming': 0.09; 'spaces': 0.09; 'subject:number': 0.09; 'python': 0.11; '1111': 0.16; 'assignment.': 0.16; 'be:': 0.16; 'columns': 0.16; 'slice.': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'input': 0.22; 'example': 0.22; 'python?': 0.22; 'print': 0.22; 'header :User-Agent:1': 0.23; 'integer': 0.24; 'equivalent': 0.26; 'header :In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; "i'm": 0.30; '(which': 0.31; 'decimal': 0.31; 'supposed': 0.32; 'regular': 0.32; 'convert': 0.35; 'there': 0.35; 'curious': 0.36; 'doing': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'easy': 0.60; 'you.': 0.62; 'email addr:gmail.com': 0.63; 'received:74.208': 0.68; 'results': 0.69; '0000': 0.84; 'pad': 0.84; 'presumably': 0.84; 'received:74.208.4.194': 0.84; 'str.': 0.91
Date Thu, 23 May 2013 07:55:36 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6
MIME-Version 1.0
To python-list@python.org
Subject Re: help in obtaining binary equivalent of a decimal number in python
References <7f794f03-03c9-44de-8312-4ad33b68c266@googlegroups.com>
In-Reply-To <7f794f03-03c9-44de-8312-4ad33b68c266@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:ZkCLL4RALNxVA3iVIAZtdtkFMLcEHuV5NGgWG+sEzLh 65ANbxUfSj5Wd3KY0HtqVmbuBMlJCTdcEUiJaN1InT3p9GEnfa EdGEE6o7RsF/0BrjBNdQjbUD2pt3YEvhPxSqeScVIYU+55O67W VNqZ3Uq9BB4kZRCavjoOPSfr9RzAIKPqH2vc1kYZb97h0c/SdS mVj6I5ZVQ9WqtT6Gze+TA+sYQPNtCmcL0iMel4knHl69NiXxJL E1el9rkJboNI75JpNvmRlxC+Pim0cbTLZ2nNcf0cf/tURg3UM4 7PBskphH8xK+LbRYCCBeP7NtVwQ838C1f7iYqlQ9oQvv3d9+ba bCj/TJDRdakImBXv9S0E=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2014.1369310150.3114.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1369310150 news.xs4all.nl 15864 [2001:888:2000:d::a6]:56403
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45803

Show key headers only | View raw


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 | NextPrevious in thread | Find similar | Unroll thread


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