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


Groups > comp.lang.python > #98620

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: bitwise operator, bits dont go into bitbucket..?
Date Wed, 11 Nov 2015 10:16:17 +1100
Lines 81
Message-ID <mailman.226.1447197380.16136.python-list@python.org> (permalink)
References <20151110222721.GA18905@z-sverige.nu> <CAPTjJmo36yPX1CaUmY8zPgcwR-uJgsFbD7i1ctk4bdKYY-hzKw@mail.gmail.com> <20151110225627.GA19339@z-sverige.nu>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de shvubD6DVFwDl3WWeJB1Vgi87uSeLkVbsXCEzEblsO6g==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'operator': 0.03; 'root': 0.04; 'mask': 0.07; 'works.': 0.07; 'cc:addr:python-list': 0.09; 'bits.': 0.09; 'constants.': 0.09; 'effect.': 0.09; 'integers': 0.09; 'logic': 0.09; 'subject:into': 0.09; 'successive': 0.09; "they've": 0.09; 'untouched.': 0.09; 'writable': 0.09; 'wrong,': 0.09; 'wed,': 0.15; '(result': 0.16; 'bit"': 0.16; 'bits,': 0.16; 'constants': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instance:': 0.16; 'operator.': 0.16; 'read?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'set,': 0.16; 'shifts': 0.16; 'zero,': 0.16; 'wrote:': 0.16; 'integer': 0.18; 'widget': 0.18; 'thanks.': 0.18; '>>>': 0.20; '(not': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'constant': 0.22; 'shifting': 0.22; 'am,': 0.23; 'code.': 0.23; 'leave': 0.23; 'bit': 0.23; 'sets': 0.23; 'this:': 0.23; 'import': 0.24; '(this': 0.24; 'header:In-Reply-To:1': 0.24; 'testing': 0.25; 'not.': 0.27; 'message-id:@mail.gmail.com': 0.27; '32-bit': 0.29; 'table,': 0.29; 'class': 0.33; 'common': 0.33; 'foo': 0.33; 'right?': 0.33; '(for': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'could': 0.35; 'nov': 0.35; 'set.': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'assigned': 0.36; 'subject:: ': 0.37; 'turn': 0.37; 'received:209.85.213': 0.37; 'wanted': 0.37; 'things': 0.38; 'received:209': 0.38; 'skip:o 20': 0.38; 'test': 0.39; 'does': 0.39; 'some': 0.40; 'your': 0.60; 'matter': 0.63; 'improved': 0.63; 'is.': 0.63; 'world': 0.64; 'obvious': 0.76; 'chrisa': 0.84; 'kent': 0.84; 'you;': 0.84; 'to:none': 0.91; 'abc': 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=Eyu8VCI9wCL8enCb/i2S4C/1Y4ACpJ5SqkEfHW43vIM=; b=JU/wMZ9jGVHoJGmZ9P2Cmqfr9qjYhR4qkRWmkNlaKmxUrvc0rIbzvP1a6kJWV2D6dG BZAfeglsDsn5PpMSmtwV5aGcqhE39NfclQ/GhPhiIJjDhv2pzPkf0OZqSExs0y3HQ+/G VxxhJfL/Kjc6zRn9Fs18/Gys7fFnHi/b6/cXMjgnxiZpO82bqWSGL9nm8VqGBKh9g5on Xg+6G0p6t62E4BURDKNewg8CLsBEKG17/8qjNagX8sgRZycsRiqE11v282YQckdL4X77 znCWFI+D04tnhhGHG1vgLw1pstHLlbQW8hjoMgl+d1uCZrcGhl0YMFZMWRmGIQKtNil9 H+bQ==
X-Received by 10.50.183.11 with SMTP id ei11mr7032163igc.94.1447197377198; Tue, 10 Nov 2015 15:16:17 -0800 (PST)
In-Reply-To <20151110225627.GA19339@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 <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:98620

Show key headers only | View raw


On Wed, Nov 11, 2015 at 9:56 AM, kent nyberg <kent@z-sverige.nu> wrote:
> 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.

Exactly! It's common to have named constants for things that matter to
you; for example, you can find out if a file/directory is
world-writable like this:

>>> import stat
>>> os.stat("/").st_mode & stat.S_IWOTH
0
>>> os.stat("/tmp").st_mode & stat.S_IWOTH
2

So my root directory is not world writable (result of zero), and the
/tmp directory is. (This is not surprising.) The S_IWOTH constant is
simply the integer 2, but giving it a name makes it a bit easier to
see what's going on in the code. (Not that the name is abundantly
clear... it could be improved on. But it's better than just '2'.)

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

That's true of right shifting, but when you left shift, you're
depending on a specific word size. Even in C, it's possible to get
burned by that (eg when you move from 32-bit to 64-bit), and in
Python, integers have infinite size. If you definitely want that
behaviour, you can do this:

(0b01000 << 1) & 0b11111

which will mask off your result to just five bits, giving you the
"drop the top bit" effect. But for bit testing and manipulation, it's
way easier to use AND/OR/XOR than shifts.

The one place you might want to use bit shifts is in _creating_ those
constants. For instance:

STYLE_FOO = 1 << 0
STYLE_BAR = 1 << 1
STYLE_BAZ = 1 << 2
STYLE_ABC = 1 << 3
STYLE_QWE = 1 << 4
STYLE_XYZ = 1 << 5

>From this table, it's obvious that they've been assigned successive
bits. You can do the same with an enumeration:

>>> class Style(enum.IntEnum):
...     FOO = 1 << 0
...     BAR = 1 << 1
...     BAZ = 1 << 2
...     ABC = 1 << 3
...     QWE = 1 << 4
...     XYZ = 1 << 5
...
>>> Style.BAR
<Style.BAR: 2>
>>> Style.QWE
<Style.QWE: 16>

And then you can test to see if some style was chosen:

>>> widget = Style.FOO | Style.QWE
>>> widget & Style.ABC
0
>>> widget & Style.QWE
16

Does that make things a bit easier to read?

ChrisA

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


Thread

Re: bitwise operator, bits dont go into bitbucket..? Chris Angelico <rosuav@gmail.com> - 2015-11-11 10:16 +1100

csiph-web