Bitwise Operations

Chris Angelico rosuav at gmail.com
Mon Jul 29 20:07:46 EDT 2013


On Tue, Jul 30, 2013 at 12:48 AM, Devyn Collier Johnson
<devyncjohnson at 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



More information about the Python-list mailing list