Velocity Reviews > Bitwise Operations

# Bitwise Operations

Devyn Collier Johnson
Guest
Posts: n/a

 07-29-2013
On Python3, how can I perform bitwise operations? For instance, I want
something that will 'and', 'or', and 'xor' a binary integer.

Mahalo,

http://www.velocityreviews.com/forums/(E-Mail Removed)

Grant Edwards
Guest
Posts: n/a

 07-29-2013
On 2013-07-29, Devyn Collier Johnson <(E-Mail Removed)> wrote:

> On Python3, how can I perform bitwise operations? For instance, I want
> something that will 'and', 'or', and 'xor' a binary integer.

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

Devyn Collier Johnson
Guest
Posts: n/a

 07-29-2013

On 07/29/2013 05:53 PM, Grant Edwards wrote:
> On 2013-07-29, Devyn Collier Johnson <(E-Mail Removed)> wrote:
>
>> On Python3, how can I perform bitwise operations? For instance, I want
>> something that will 'and', 'or', and 'xor' a binary integer.

>

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

Ethan Furman
Guest
Posts: n/a

 07-29-2013
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 <(E-Mail Removed)> wrote:
>>
>>> On Python3, how can I perform bitwise operations? For instance, I want
>>> something that will 'and', 'or', and 'xor' a binary integer.

>>

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

Chris Angelico
Guest
Posts: n/a

 07-29-2013
On Tue, Jul 30, 2013 at 12:34 AM, Devyn Collier Johnson
<(E-Mail Removed)> 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 , 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

Devyn Collier Johnson
Guest
Posts: n/a

 07-29-2013

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 <(E-Mail Removed)> wrote:
>>>
>>>> On Python3, how can I perform bitwise operations? For instance, I want
>>>> something that will 'and', 'or', and 'xor' a binary integer.
>>>

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

Chris Angelico
Guest
Posts: n/a

 07-30-2013
On Tue, Jul 30, 2013 at 12:48 AM, Devyn Collier Johnson
<(E-Mail Removed)> 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

MRAB
Guest
Posts: n/a

 07-30-2013
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 <(E-Mail Removed)> wrote:
>>
>>> On Python3, how can I perform bitwise operations? For instance, I want
>>> something that will 'and', 'or', and 'xor' a binary integer.

>>

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

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

Terry Reedy
Guest
Posts: n/a

 07-30-2013
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

Ulrich Eckhardt
Guest
Posts: n/a

 07-30-2013
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