[Python-ideas] Bitwise operations on bytes class
MRAB
python at mrabarnett.plus.com
Wed Jun 18 01:30:36 CEST 2014
On 2014-06-17 23:10, Nick Coghlan wrote:
>
> On 18 Jun 2014 07:34, "MRAB" <python at mrabarnett.plus.com> wrote:
> >
> > On 2014-06-17 21:37, Antoine Pitrou wrote:
> >>
> >> Le 17/06/2014 15:35, Ethan Furman a écrit :
> >>>
> >>>
> >>> I like the idea, but one question I have: when shifting, are the
> >>> incoming bits set to 0 or 1? Why?
> >>
> >>
> >> By convention, 0. Historically, that's how CPUs do it.
> >> (and also because it provides a quick way of multiplying /
dividing by 2^N).
> >>
> > That's sometimes known as a "logical shift".
>
> My bitbashing-with-Python work was all serial communications protocol
based, so logical shifts were what I wanted (I was also in the fortunate
position of being able to tolerate the slow speed of doing them in
Python, because HF radio comms are so slow the data streams to be
analysed weren't very big).
>
> > When shifting to the right, there's also the "arithmetic shift", which
> > preserves the most significant bit.
> >
> > Do we need that too? (I don't think so.) If yes, then what should be
> > operator be? Just a 'normal' method call?
>
> Wanting an arithmetic shift would be a sign that one is working with
integers rather than arbitrary binary data, and ints or one of the fixed
width types from NumPy would likely be a better fit. So leaving that out
of any proposal sounds fine to me.
>
What about rotates?
More information about the Python-ideas
mailing list