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


Groups > comp.lang.python > #51506 > unrolled thread

Bitwise Operations

Started byDevyn Collier Johnson <devyncjohnson@gmail.com>
First post2013-07-29 17:25 -0400
Last post2013-07-30 01:46 -0400
Articles 10 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  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

#51506 — Bitwise Operations

FromDevyn Collier Johnson <devyncjohnson@gmail.com>
Date2013-07-29 17:25 -0400
SubjectBitwise Operations
Message-ID<mailman.5275.1375134621.3114.python-list@python.org>
On Python3, how can I perform bitwise operations? For instance, I want 
something that will 'and', 'or', and 'xor' a binary integer.

Mahalo,

DevynCJohnson@Gmail.com

[toc] | [next] | [standalone]


#51507

FromGrant Edwards <invalid@invalid.invalid>
Date2013-07-29 21:53 +0000
Message-ID<kt6o8t$l55$1@reader1.panix.com>
In reply to#51506
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

-- 
Grant Edwards               grant.b.edwards        Yow! I have the power to
                                  at               HALT PRODUCTION on all
                              gmail.com            TEENAGE SEX COMEDIES!!

[toc] | [prev] | [next] | [standalone]


#51521

FromDevyn Collier Johnson <devyncjohnson@gmail.com>
Date2013-07-29 19:34 -0400
Message-ID<mailman.5286.1375140856.3114.python-list@python.org>
In reply to#51507
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.

Mahalo,

DCJ

[toc] | [prev] | [next] | [standalone]


#51547

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2013-07-30 08:20 +0200
Message-ID<mh2kca-hij.ln1@satorlaser.homedns.org>
In reply to#51521
Am 30.07.2013 01:34, schrieb Devyn Collier Johnson:
> Typing "101 & 010" or "x = (int(101, 2) & int(010, 2))" only gives errors.

What errors? Check out Eric Raymond's essay on asking smart questions, 
it's a real eye-opener! ;)

That said, use "0b" as prefix for binary number literals (0b1000 is 
eight, for example).

Cheers!

Uli

[toc] | [prev] | [next] | [standalone]


#51522

FromEthan Furman <ethan@stoneleaf.us>
Date2013-07-29 16:41 -0700
Message-ID<mailman.5287.1375141301.3114.python-list@python.org>
In reply to#51507
On 07/29/2013 04:34 PM, 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.

x = (int('101', 2) & int('010', 2))

Notice the quotes.

In the future you'll better answers quicker if you tell us what you did (such as your example above) as well as the errors.

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#51524

FromChris Angelico <rosuav@gmail.com>
Date2013-07-30 00:44 +0100
Message-ID<mailman.5289.1375141462.3114.python-list@python.org>
In reply to#51507
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

[toc] | [prev] | [next] | [standalone]


#51526

FromDevyn Collier Johnson <devyncjohnson@gmail.com>
Date2013-07-29 19:48 -0400
Message-ID<mailman.5291.1375141726.3114.python-list@python.org>
In reply to#51507
On 07/29/2013 07:41 PM, Ethan Furman wrote:
> On 07/29/2013 04:34 PM, 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.
>
> x = (int('101', 2) & int('010', 2))
>
> Notice the quotes.
>
> In the future you'll better answers quicker if you tell us what you 
> did (such as your example above) as well as the errors.
>
> -- 
> ~Ethan~
Thanks Ethan for the code help and the tip. I need to get out of that 
habit of not including errors. This code works well, thanks! I cannot 
believe I was that close to the solution!

Now here is something that confuses me, the binary numbers are numbers 
not strings, so why are they put in quotes as if they are strings?


Mahalo,

DCJ

[toc] | [prev] | [next] | [standalone]


#51529

FromChris Angelico <rosuav@gmail.com>
Date2013-07-30 01:07 +0100
Message-ID<mailman.5294.1375142869.3114.python-list@python.org>
In reply to#51507
On Tue, Jul 30, 2013 at 12:48 AM, Devyn Collier Johnson
<devyncjohnson@gmail.com> wrote:
> Now here is something that confuses me, the binary numbers are numbers not
> strings, so why are they put in quotes as if they are strings?

They aren't numbers at that point, they're strings of digits. A number
is represented in various forms:

>>> 1234, 0x4d2, 0o2322, 0b10011010010
(1234, 1234, 1234, 1234)

The two-argument form of int() takes a string of digits and a base:

>>> int("ya",36)
1234

In base 36, y and a are digits. But in Python's base syntax, you can't
use them that way, so there's no way to render the number other than
as a string.

For what you're doing, I think the 0b notation is the best. It's an
int literal written in binary.

ChrisA

[toc] | [prev] | [next] | [standalone]


#51536

FromMRAB <python@mrabarnett.plus.com>
Date2013-07-30 02:55 +0100
Message-ID<mailman.5297.1375149355.3114.python-list@python.org>
In reply to#51507
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

[toc] | [prev] | [next] | [standalone]


#51544

FromTerry Reedy <tjreedy@udel.edu>
Date2013-07-30 01:46 -0400
Message-ID<mailman.5301.1375163230.3114.python-list@python.org>
In reply to#51507
On 7/29/2013 7:44 PM, Chris Angelico wrote:

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

 >>> bin(0b101 | 0b010)
'0b111'

Now you are set to go. Have fun.
-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web