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


Groups > comp.lang.python > #51536

Re: Bitwise Operations

Date 2013-07-30 02:55 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Bitwise Operations
References <mailman.5275.1375134621.3114.python-list@python.org> <kt6o8t$l55$1@reader1.panix.com> <51F6FBE8.8000106@Gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5297.1375149355.3114.python-list@python.org> (permalink)

Show all headers | View raw


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 <devyncjohnson@gmail.com> 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

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