[Python-ideas] Bitwise operations on bytes class

Nathaniel McCallum npmccallum at redhat.com
Mon Jun 16 21:43:33 CEST 2014


On Mon, 2014-06-16 at 15:20 -0400, Terry Reedy wrote:
> On 6/16/2014 2:03 PM, Nathaniel McCallum wrote:
> > I find myself, fairly often, needing to perform bitwise operations
> > (rshift, lshift, and, or, xor) on arrays of bytes in python (both bytes
> > and bytearray).
> 
> If you are often doing and/or/xor on large arrays, as one might do for 
> bitmap images, you should probably be using numpy or a derivative thereof.
> 
> What use do you have for shifting bits across byte boundaries, where the 
> bytes are really bytes?  Why would you not turn multiple bytes 
> considered together into an int?

There are many reasons. Anything relating to cryptography, key
derivation, asn1 BitString, etc. Many network protocols have specialized
algorithms which require bit rotations or bitwise operations on blocks.

> > I can't think of any other reasonable use for these operators.
> 
> I don't understand this. They are routinely used on ints for various 
> purposes.

I meant that, for instance, I can't think of any other reasonable
interpretation for what "bytes() ^ bytes()" would mean other than a
bitwise xor of the bytes in the arrays. Yes, of course the operators
have meanings in other contexts. But in this context, I think the
meaning of the operators is self-evident and precise in meaning.

Perhaps some code will clarify what I'm proposing. Attached is a class I
have found continual reuse for over the last few years. It implements
bitwise operators on a bytes subclass. Something similar could be done
for bytearray.

Nathaniel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bbytes.py
Type: text/x-python
Size: 1571 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140616/2aa5e062/attachment.py>


More information about the Python-ideas mailing list