[Python-ideas] bitwise operations on bytes

Mark Dickinson dickinsm at gmail.com
Fri Aug 7 15:19:20 CEST 2009

On Fri, Aug 7, 2009 at 11:04 AM, Eric Eisner<ede at mit.edu> wrote:
> Hello,
> As previously mentioned on python-ideas [1] (circa 2006), it would
> make sense to be able to perform bitwise operations on
> bytes/bytearray.

+1 from me.  I'd say that it makes just as much sense to do bit
operations on bytes or bytearrays as it does on integers.
I've always felt a little bit dirty when I've abused ints as
bitstrings in the past;  byte strings seem better suited to this
task, especially where input and output are also involved.

> Unresolved problems:
> If the two arguments are of different length, either it could either
> raise a ValueError or mimic the behavior of ints.

I'd say ValueError, since it's not really clear what 'mimic the
behaviour of ints' means here.  E.g., should

bytes([1,2,3,4]) | bytes([16, 32, 64])

be equal to bytes([17, 34, 67, 64]) or bytes([1, 18, 35, 68]).
It seems better to restrict the use to the cases where
there's a single obvious interpretation.

> Xoring an int to a byte seems less than well defined in general, due
> to endianness ambiguity of the int and size ambiguity. I would think
> this should not be allowed.


> Also conceivable is using the shift operators >> and << on bytes, but
> I personally would use that less often, and the result of such an
> operation is ambiguous due to endianness.

Agreed.  To make sense of the shift operators you effectively have
to give 'position' interpretations for the individual bits, and there's no
single obvious way of doing this;  for the plain bitwise operations
this isn't necessary.

One other question:  if a has type bytes and b has type bytearray,
what types would you propose that a & b and b & a should have?


More information about the Python-ideas mailing list