Hey @zep, me again. ;)
I looked through the rather comprehensive list at https://rosettacode.org/wiki/Bitwise_operations and couldn't spot anyone at all who had an operator for rotation, let alone find some kind of standard for it. Most languages use either a function or a textual operator (e.g. a ror b), but those end up making your expressions a lot larger and/or filled with whitespace.
WE COULD BE THE ONES TO SET THE STANDARD! :D
I'll just propose a few ideas. Note that I bring the "|" operator into some of them because normally people have to do rotates by or'ing together two shifted values.
rotl rotr ---------- ---------- a <<<< b a >>>> b a <<> b a <>> b -- I like these best, personally a >|> b a <|< b a |>> b a <<| b a >>| b a |<< b |
I like <<> and <>> because they imply that part of the shift wraps back around. I also find them aesthetically pleasing.
Also, if this happens, please remember assignment operators and metamethods? :)
Those would be ambiguous with a second value that's prefixed with a bitwise-not.
a >> ~b
a >>~ b
But if you put the ~ in front it'd be unambiguous.
a ~>> b
a ~<< b
Oh, right - >>~/<<~ would break existing carts that use a>>~b as a>>(~b)
I really like this idea on the surface, and the look of <<> >><
The reason I was put off adding operators for these though is that currently, it's not /too/ hard to automatically convert a PICO-8 program into a legal Lua 5.3 one, using only simple pre-processor transformations. But adding rotl, rotr operators would require something at the parser level. The oddities of PICO-8's syntax, as disruptive as they are, are still intended to be vaguely cooperative with the larger software world where possible.
Okie dokie. I won't press for this one, since no other language really has it either. But if you ever change your mind, I'll be right there cheering and throwing confetti. ;)
In light of that, though, could the rotl()/rotr() functions be cheaper? Right now they cost 6x what the corresponding shift operator does, and 2x what it would cost to do the rotate inline with two shifts and an or. At the very least it shouldn't cost more than the latter.
@zep - how's >>< different from >>>? There's no corresponding operator in lua 5.3 so you'd have to convert it to a function in both cases.
Also, I think a parser is much easier to write than a correct preprocessor. (Is the pico8 preprocessor implementation correct yet?)
[Please log in to post a comment]