Bitwise Operations

  • Thread starter Devyn Collier Johnson
  • Start date
D

Devyn Collier Johnson

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

Mahalo,

(e-mail address removed)
 
E

Ethan Furman

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

Chris Angelico

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

But there's an easier way:
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
 
D

Devyn Collier Johnson

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

Chris Angelico

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, 1234, 1234, 1234)

The two-argument form of int() takes a string of digits and a base:
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
 
M

MRAB

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
 
T

Terry Reedy

But there's an easier way:

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.
'0b111'

Now you are set to go. Have fun.
 
U

Ulrich Eckhardt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Share Code: Laptop Lid State 0
Cross-Platform Python3 Equivalent to notify-send 1
Play Ogg Files 0
Critic my module 13
List as Contributor 0
Aloha! Check out the Betabots! 0
Share Code Tips 13
PEP8 79 char max 3

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top