[Python-ideas] bitwise operations on bytes

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Aug 9 00:41:55 CEST 2009

Guido van Rossum wrote:
> I think it's a very rarely used feature that is more likely to baffle
> the casual reader.

However there's currently no way to efficiently do
bitwise operations en masse on raw bytes without
converting to and from long integers, which doesn't
seem very satisfactory. Refusing to provide this
ability for the above reason sounds like purity
beating practicality to me.

> The question of what to do with byte strings of
> unequal length is just the first issue that crops up.

I would raise an exception.

> The (still
> legitimate) question why we would support | and & but not << and >> is
> another.

Ambiguity due to byte order seems like a good enough
reason not to implement shifts as operators. Maybe
provide methods that specify a byte order explicitly?


More information about the Python-ideas mailing list