[Python-ideas] bitwise operations on bytes

Nick Coghlan ncoghlan at gmail.com
Fri Aug 7 14:24:01 CEST 2009

Tal Einat wrote:
> You can already do bitwise operations on bytes, and retrieving the
> relevant byte and using single-byte operations on it is simple enough.
> As I see it, the major underlying problem here is that byte-arrays are
> immutable, since what you really want is to be able to change a single
> byte of the array in-place. In general, Python's byte arrays and
> strings are very ill suited for dealing with data which isn't
> byte-based, and definitely horrible at building or modifying data
> streams.

Careful with your terminology here. In Python 3.x, the following two
types both exist:

bytes: immutable sequence of integers between 0 and 255 inclusive
bytearray: similar to bytes, but mutable

The operations ~x, x&y, x|y and x^y make sense for both bytes and bytearray.

The augmented assignment operators only make sense for bytearray.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list