[Python-ideas] bitwise operations on bytes

Eric Eisner ede at mit.edu
Sun Aug 9 09:06:32 CEST 2009

> In the grander scheme of things, I worry that interpreting byte
> strings as integers and implementing bitwise operators on them is
> going to cause more confusion and isn't generally useful enough to
> warrant the extra code. I'd be okay with a standard API to transform a
> byte array into an integer and vice versa -- there you can be explicit
> about byte order and what to do about negative numbers.

For some background, I think the separation of text and data is a
powerful idea, and now that I have my nice builtin object for data
(and its mutable cousin), I want to be able to manipulate the raw data
directly. Currently, all of the bitwise manipulations live in the int
type (for what I like to think of as historical reasons). Currently,
when manipulating a byte at a time, the bytes type does the right
thing and shows the elements as ints, giving me access to these
bitwise manipulations. However there currently lacks builtin
functionality to manipulate multi-bytes chunks. In the grander scheme
of things, this is what I would like to see addressed.

The most general solution, which Guido mentions, and which I have also
been thinking about, is a builtin conversion from bytes to ints that
explicitly requires resolving ambiguities (eg endianness, negatives,
data length). I personally use those pretty gross hacks to fake the
functionality now, but they assumes big-endian, two's complement etc.

I proposed direct bitwise operators (~ & ^ |) for bytes because these
seemed to be the subset of all bitwise manipulations that were
(mostly) unambiguous for raw bytes. Even if python gains conversions
between bytes and ints, I still think these unambiguous operations
would be useful to have (especially ~, the most unambiguous among


More information about the Python-ideas mailing list