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


Groups > comp.lang.python > #98619

Re: bitwise operator, bits dont go into bitbucket..?

Path csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!tfh-berlin.DE!fu-berlin.de!uni-berlin.de!not-for-mail
From kent nyberg <kent@z-sverige.nu>
Newsgroups comp.lang.python
Subject Re: bitwise operator, bits dont go into bitbucket..?
Date Tue, 10 Nov 2015 17:56:27 -0500
Lines 34
Message-ID <mailman.225.1447196189.16136.python-list@python.org> (permalink)
References <20151110222721.GA18905@z-sverige.nu> <CAPTjJmo36yPX1CaUmY8zPgcwR-uJgsFbD7i1ctk4bdKYY-hzKw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace news.uni-berlin.de TgWIJu3Oz72CUAZE4dLAMQdDTFF72pQ68PSBICVQYqDA==
Return-Path <kent@z-sverige.nu>
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; 'python,': 0.02; 'operator': 0.03; 'bits': 0.07; 'works.': 0.07; 'logic': 0.09; 'subject:into': 0.09; 'untouched.': 0.09; 'wrong,': 0.09; 'wed,': 0.15; 'bitwise': 0.16; 'operator.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'set)': 0.16; 'set,': 0.16; 'zero,': 0.16; 'wrote:': 0.16; '(in': 0.18; 'thanks.': 0.18; '>>>': 0.20; '2015': 0.20; 'shifting': 0.22; 'am,': 0.23; 'leave': 0.23; 'bit': 0.23; 'sets': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'chris': 0.26; 'not.': 0.27; 'url:mailman': 0.30; 'url:python': 0.33; 'common': 0.33; 'right?': 0.33; 'url:listinfo': 0.34; '(for': 0.34; 'so,': 0.35; 'could': 0.35; 'nov': 0.35; 'set.': 0.35; 'but': 0.36; 'url:org': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'turn': 0.37; 'charset :us-ascii': 0.37; 'wanted': 0.37; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'side': 0.62; 'more': 0.63; 'received:178': 0.72; 'hand': 0.82; 'chrisa': 0.84; 'kent': 0.84
Content-Disposition inline
In-Reply-To <CAPTjJmo36yPX1CaUmY8zPgcwR-uJgsFbD7i1ctk4bdKYY-hzKw@mail.gmail.com>
User-Agent Mutt/1.5.23 (2014-03-12)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98619

Show key headers only | View raw


On Wed, Nov 11, 2015 at 09:33:38AM +1100, Chris Angelico wrote:
> On Wed, Nov 11, 2015 at 9:27 AM, kent nyberg <kent@z-sverige.nu> wrote:

> 
> 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
> 

So, to check if 0b010[this one bit]010 is set,   i do   & 0b0001000

That is,  I set just that one to 1 in the other and then the & operator will make it return
0 if its not set. Since every other is zero, the return will be zero if its not. Since & operator sets 0
if not both are 1. Right?  

Id so, Thanks.   

My misunderstanding was that
0b01000   (for example,) first could be shifted left. To become 0b10000.
And then shifted right to become 0b00001. 
The shifting would turn every other digit to 0 and leave only the wanted one untouched. 
That way, I could check if its 0 or 1.  If you understand my own logic of how it works.
But I got it wrong,  and I think I know how to do it with & operator.

> 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
> -- 
> https://mail.python.org/mailman/listinfo/python-list

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: bitwise operator, bits dont go into bitbucket..? kent nyberg <kent@z-sverige.nu> - 2015-11-10 17:56 -0500

csiph-web