[Python-ideas] String and bytes bitwise operations

Steven D'Aprano steve at pearwood.info
Sat May 19 09:52:29 EDT 2018


On Fri, May 18, 2018 at 06:25:19PM -0400, Chris Barker - NOAA Federal via Python-ideas wrote:

> > I suppose you could argue that a "byte" is a patch of
> > storage capable of holding a number from 0 to 255, as opposed to being
> > the number itself, but that's getting rather existential :)
> 
> No, I’m making the distinction that an eight bit byte is, well,  eight
> bits, that CAN represent a number from 0 to 255, or it can represent
> any other data type — like one eighth of the bits in a float, for
> instance. Or a bit field, or 1/2 a 16 bit int.

That might be the case if we were talking about chunks of memory in RAM, 
but we're talking about *bytes objects in Python* where the individual 
items in the object most certainly represent ints between 0 and 255:

py> b"abcde"[3]
100


Philosophical arguments about the nature of computer memory aside, byte 
objects in Python are collections of ints.

If you want those ints to represent something else, you're responsible 
for handling that (say, using the struct module).


-- 
Steve


More information about the Python-ideas mailing list