[Python-ideas] Bitwise operations on bytes class

Nick Coghlan ncoghlan at gmail.com
Tue Jun 17 00:48:51 CEST 2014


On 17 Jun 2014 05:44, "Nathaniel McCallum" <npmccallum at redhat.com> wrote:
>
> 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 used to want something like this when trying to deal with bit slips on
serial channels - sliding a pattern one bit to the left or right was a pain.

It makes more sense on the bytes type to me than it does on multibyte array
formats (which would suffer from messy endianness issues).

As Nathaniel noted, there's no other obvious meaning for these operations
on the binary data types, and it would definitely make bitbashing in Python
easier (something that will only become more common with the rise of things
like Arduino, Raspberry Pi and MicroPython).

Cheers,
Nick.

> > > 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
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140617/1c6a4152/attachment.html>


More information about the Python-ideas mailing list