[Python-ideas] Bitwise operations on bytes class
Nathaniel McCallum
npmccallum at redhat.com
Wed Jun 18 17:35:28 CEST 2014
On Mon, 2014-06-16 at 14:03 -0400, 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). I can't think of any other reasonable use for these
> operators. Is upstream Python interested in this kind of behavior by
> default? At the least, it would make many algorithms very easy to read
> and write.
So it seems to me that there is a consensus that something like this is
a good idea, with perhaps the exception of Guido's reminder to not
overpopulate the operators (is that a no for this proposal?).
Summarizing:
1. In lshift, what bits are introduced on the right-hand side? Zero is
traditional.
2. In rshift, what bits are introduced on the left-hand side? An
argument can be made for either zero (logical) or retaining the
left-most bit (arithmetic). The 'arithmetic shift' seems to fit the
sphere of NumPy. Zero should be preferred.
3. Rotates and other common operations are out of scope for this
proposal.
4. One question not discussed is what to do when attempting to
and/or/xor against a bytes() or bytearray() that is of a different
length. Should we left-align the shorter of the two? Right-align? Throw
an exception?
Also, I'm new to this process. Where should I go from here? Do I need to
form a PEP?
Nathaniel
More information about the Python-ideas
mailing list