Log In  

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? :)

P#74985 2020-04-19 16:52 ( Edited 2020-04-19 16:55)

Alternatively:
>>~ <<~

P#75024 2020-04-20 06:12 ( Edited 2020-04-20 06:13)

+1

P#75026 2020-04-20 06:44

@thisismypassword

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

P#75029 2020-04-20 09:03

Oh, right - >>~/<<~ would break existing carts that use a>>~b as a>>(~b)

P#75032 2020-04-20 09:27

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.

P#75086 2020-04-21 00:59

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.

P#75095 2020-04-21 05:01 ( Edited 2020-04-21 05:04)

and we promise to not abuse it ;)

P#75098 2020-04-21 05:54

@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?)

P#75099 2020-04-21 05:54 ( Edited 2020-04-21 05:55)

[Please log in to post a comment]