Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: bitwise operator, bits dont go into bitbucket..? Date: Wed, 11 Nov 2015 09:33:38 +1100 Lines: 30 Message-ID: References: <20151110222721.GA18905@z-sverige.nu> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de g48VMYE4WajSegre8eFUXgtZxud8sYJlYu3CxXGbiriQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'bits': 0.07; 'cc:addr:python-list': 0.09; 'subject:into': 0.09; 'python': 0.10; 'wed,': 0.15; '(when': 0.16; 'bitbucket.': 0.16; 'bitwise': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'set)': 0.16; 'then?': 0.16; 'wrote:': 0.16; 'integer': 0.18; '(in': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'shifting': 0.22; 'am,': 0.23; 'bit': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'away.': 0.29; 'operators': 0.29; "i'm": 0.30; 'code': 0.30; 'common': 0.33; 'add': 0.34; 'gets': 0.35; 'gives': 0.35; 'received:google.com': 0.35; 'nov': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'received:209.85.213': 0.37; 'seem': 0.37; 'doing': 0.38; 'received:209': 0.38; 'sure': 0.39; 'called': 0.40; 'some': 0.40; 'side': 0.62; 'more': 0.63; 'dont': 0.64; 'results': 0.66; '100%': 0.72; 'hand': 0.82; 'chrisa': 0.84; 'kent': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=dwYDj+uDOUANDy5ywmk6Tnlgwrq4toAMhjUtxlpoXjY=; b=f+trXzCbMgaRPb16JZatPdo60UMHol8RZFXnS9csC70uw3YlHh2v/9zv/zZ2JwBSPS vV+CoqvZP/t6DVKZYlq3xkJhs6VSVqc0/MTDpC0jEocT5Iob/ucrZC+jMdp/r2JTsWI8 JxxmPakyvOXFYHs4uAm0fnZ4wKLfOR6gQF1q3X0DMol59NhuJRXINYYEHTkA9HL79XpA PNxSSSd7RyNtVeuYnOQi9wUP8Z/v3Jyxa1xWvTR1BQkj7+/UdqrOibrph+ckITnJQaDj zO/z6bljz7CJ70ixmZWFt/2y+zjlcHipnX5Iqj4YtWgjoIkqakH6GlYkJCdQkPv82yNH dCAQ== X-Received: by 10.50.155.33 with SMTP id vt1mr6750294igb.92.1447194818784; Tue, 10 Nov 2015 14:33:38 -0800 (PST) In-Reply-To: <20151110222721.GA18905@z-sverige.nu> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98614 On Wed, Nov 11, 2015 at 9:27 AM, kent nyberg wrote: > Im reading about bitwise operators and is it true to say they dont work 100% as in C? > bitwise operators in C seem to result in bits going to the so called bitbucket. > For example, 0b00000001. Shifting it >> 1 in C it seems to add on zero to the left and the 1 to the right gets throwned away. > > But doing it in python just adds one more bit, from the left. > That is, 0b00000001 >> 1 = 0b000000001. I'm not sure what you're expecting Python to do here, but right-shifting the integer 1 results in the integer 0: >>> 0b000001 >> 1 0 > Bitwise operators in C (when reading examples,) gives some time code that check specific bits by > shifting the bits left and right to make every bit but the specific one to zeros. > As I understand bitwise operators in python, this is not possible then? If you want to check specific bits (in C or Python, either way), it's much more common to use bitwise AND than bit shifts: >>> 0b100011011101010110 & 0b000000010000 16 >>> print(bin(_)) 0b10000 This will be either the same number as the right hand side (if the bit had been set) or zero (if it hadn't). ChrisA