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


Groups > comp.lang.python > #51524

Re: Bitwise Operations

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; 'error:': 0.07; 'explicit': 0.07; 'string': 0.09; '101': 0.09; 'collier': 0.09; 'function,': 0.09; 'interpreted': 0.09; 'latter': 0.09; 'way:': 0.09; 'python': 0.11; 'bitwise': 0.16; 'command.': 0.16; 'consciously': 0.16; 'digits.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'literal,': 0.16; 'literals.': 0.16; 'non-string': 0.16; 'octal': 0.16; 'operators,': 0.16; 'typeerror:': 0.16; 'values:': 0.16; 'wrote:': 0.18; 'typing': 0.19; '>>>': 0.22; 'errors.': 0.24; 'integer': 0.24; 'helpful': 0.24; 'script': 0.25; 'task': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'google,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'easier': 0.31; '"",': 0.31; 'decimal': 0.31; 'fixing': 0.31; 'file': 0.32; '(most': 0.33; 'problem': 0.35; "can't": 0.35; 'convert': 0.35; 'johnson': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'shows': 0.36; 'turn': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'quote': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'how': 0.40; 'more': 0.64; '30,': 0.65; 'here': 0.66; 'jul': 0.74; 'obvious': 0.74; 'saw': 0.77; '010': 0.84; '2013': 0.98
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:to :content-type; bh=1ZVDWjasb9HO09lq036mgOtSHfeEqmuFN7S8pEQFuOQ=; b=x8kaSUL3+HnT62xsm4/O0v6i71Dqavl1W3oABETclnGoTOerf1Yqb/IfQarJL1NbWA H0xmd+Nc1nOBVaYuCnPJcr6wjLwYKZHvqA25htXIM5cZszMWNggkv6fZJimaihW5Ub8W F/uht19WUKkvh1YPL/VytK2CgJD7+iWYFooG3j737Ws9SXDki9YO1vy1P+ylo+25bCoN i2OVKvdo/MKanjf7jHGd+y7twWc9PHCq5xRbYtlu9vleRr/EMejGowcB1JprR/5pOwR+ 6z1W1Mf3WfaTcnP9pDOVuKLdBlpjQRrTGpoGxtYO994ecxV7lAFQx+GBvFGnJAoIZnAf PGZw==
MIME-Version 1.0
X-Received by 10.58.223.238 with SMTP id qx14mr25986664vec.98.1375141459614; Mon, 29 Jul 2013 16:44:19 -0700 (PDT)
In-Reply-To <51F6FBE8.8000106@Gmail.com>
References <mailman.5275.1375134621.3114.python-list@python.org> <kt6o8t$l55$1@reader1.panix.com> <51F6FBE8.8000106@Gmail.com>
Date Tue, 30 Jul 2013 00:44:19 +0100
Subject Re: Bitwise Operations
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.5289.1375141462.3114.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375141462 news.xs4all.nl 15940 [2001:888:2000:d::a6]:42174
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51524

Show key headers only | View raw


On Tue, Jul 30, 2013 at 12:34 AM, Devyn Collier Johnson
<devyncjohnson@gmail.com> wrote:
>
> 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.

Your problem here isn't in the bitwise operators, but in your binary
literals. Python deliberately and consciously rejects 010 as a
literal, because it might be interpreted either as decimal 10, or as
octal (decimal 8), the latter being C's interpretation. Fixing that
shows up a more helpful error:

>>> x = (int(101, 2) & int(10, 2))
Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    x = (int(101, 2) & int(10, 2))
TypeError: int() can't convert non-string with explicit base

The int() call isn't doing what you think it is, because 101 is
already an int. The obvious solution now is to quote the values:

>>> x = (int("101", 2) & int("010", 2))
>>> x
0

But there's an easier way:

>>> x = 0b101 & 0b010
>>> x
0

I think that might do what you want. Also check out the bin()
function, which will turn an integer into a string of digits.

ChrisA

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


Thread

Bitwise Operations Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-29 17:25 -0400
  Re: Bitwise Operations Grant Edwards <invalid@invalid.invalid> - 2013-07-29 21:53 +0000
    Re: Bitwise Operations Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-29 19:34 -0400
      Re: Bitwise Operations Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-07-30 08:20 +0200
    Re: Bitwise Operations Ethan Furman <ethan@stoneleaf.us> - 2013-07-29 16:41 -0700
    Re: Bitwise Operations Chris Angelico <rosuav@gmail.com> - 2013-07-30 00:44 +0100
    Re: Bitwise Operations Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-29 19:48 -0400
    Re: Bitwise Operations Chris Angelico <rosuav@gmail.com> - 2013-07-30 01:07 +0100
    Re: Bitwise Operations MRAB <python@mrabarnett.plus.com> - 2013-07-30 02:55 +0100
    Re: Bitwise Operations Terry Reedy <tjreedy@udel.edu> - 2013-07-30 01:46 -0400

csiph-web