[Python-ideas] Bitwise operations on bytes class

Daniel Holth dholth at gmail.com
Mon Jun 16 22:01:11 CEST 2014


Interesting idea. I like it.

I notice Python 3 has int.from_bytes() and int.to_bytes().

On Mon, Jun 16, 2014 at 3:43 PM, 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 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/


More information about the Python-ideas mailing list