Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'binary': 0.07; 'indicated': 0.07; '101': 0.09; 'collier': 0.09; 'raises': 0.09; 'python': 0.11; "'or',": 0.16; '0o,': 0.16; '0x,': 0.16; 'bitwise': 0.16; 'command.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'hexadecimal': 0.16; 'integer.': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'octal': 0.16; 'operations?': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'typing': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'errors.': 0.24; 'instance,': 0.24; 'integer': 0.24; 'switched': 0.24; 'script': 0.25; 'task': 0.26; 'header:In-Reply-To:1': 0.27; 'feature': 0.29; 'google,': 0.29; 'gives': 0.31; 'changed.': 0.31; 'decimal': 0.31; 'url:python': 0.33; 'style': 0.33; 'something': 0.35; 'johnson': 0.35; 'received:84': 0.35; 'but': 0.35; 'doing': 0.36; 'similar': 0.36; 'clear': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'such': 0.63; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'saw': 0.77; 'url:search': 0.81; '257': 0.84; 'reply- to:addr:python.org': 0.84; 'edwards': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=RZapVTdv c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=K2DDQYBT4xIA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=VgQenC37ZUEA:10 a=pGLkceISAAAA:8 a=1XWaLZrsAAAA:8 a=saI9q2Bxo7tBWDocSLsA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Tue, 30 Jul 2013 02:55:51 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Bitwise Operations References: <51F6FBE8.8000106@Gmail.com> In-Reply-To: <51F6FBE8.8000106@Gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375149355 news.xs4all.nl 15864 [2001:888:2000:d::a6]:55833 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51536 On 30/07/2013 00:34, Devyn Collier Johnson wrote: > > On 07/29/2013 05:53 PM, Grant Edwards wrote: >> On 2013-07-29, Devyn Collier Johnson wrote: >> >>> On Python3, how can I perform bitwise operations? For instance, I want >>> something that will 'and', 'or', and 'xor' a binary integer. >> http://www.google.com/search?q=python+bitwise+operations >> > I understand the symbols. I want to know how to perform the task in a > script or terminal. I have searched Google, but I never saw a command. > Typing "101 & 010" or "x = (int(101, 2) & int(010, 2))" only gives errors. > In Python 2, an integer with a leading 0, such as 0101, was octal (base 8). This was a feature borrowed from C but often confused newbies because it looked like decimal ("Why does 0101 == 101 return False?"). In Python 3, octal is indicated by a leading 0o, such as 0o101 (== 1*64+0*8+1==65) and the old style raises an exception so that those who have switched from Python 2 will get a clear message that something has changed. For binary you need a leading 0b and for hexadecimal you need a leading 0x, so doing something similar for octal makes sense. 0b101 == 1*4+0*2+0 == 5 0o101 == 1*64+0*8+1 == 65 0x101 == 1*256+0*16+1 == 257