[Python-ideas] String and bytes bitwise operations
Steven D'Aprano
steve at pearwood.info
Mon May 21 19:02:57 EDT 2018
On Mon, May 21, 2018 at 11:22:17AM -0700, Chris Barker wrote:
> On Sat, May 19, 2018 at 6:52 AM, Steven D'Aprano <steve at pearwood.info>
> wrote:
>
> > Philosophical arguments about the nature of computer memory aside, byte
> > objects in Python are collections of ints.
> >
>
> not when you start talking about bit-wise operations :-)
>
> If a "byte" in python was an integer, then we'd use b**2 rather than b << 1
Why? Ints support bit-shift operations:
py> 45 << 1
90
as well as other bitwise operations. (And b<<1 isn't the same as b**2.)
I trust that you aren't going to argue that 45 isn't an int.
> (yes, I know bit shifting can be more efficient, but I don't think that's
> why it's there in python)
>
> The entire point of bitwise operators is so that the bits themselves can be
> accessed and manipulated.
Then where are the primitives for accessing and manipulating individual bits?
> > If you want those ints to represent something else, you're responsible
> > for handling that (say, using the struct module).
>
>
> yup -- with struct, and, hmm, maybe bitwise operators?
Right. Which is why we want to add support for bitwise operators to
bytes.
> Anyway, as you say, this is a Philosophical (or semantic) point -- I don't
> think it effects the discussion at hand.
>
> However, when you talk about bit-shifting a bytes object, you do need to
> decide if each byte is handled individually, or if they are one big
> collection.
True, we have to decide that. Is there a good reason to handle each byte
individually?
--
Steve
More information about the Python-ideas
mailing list