Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl 6 equivalents to Java's >> and >>> operators?

Tags:

bit-shift

raku

I've started using Perl 6 and I'm currently porting over some code from my Java projects, and they use Java's shift operators << and >>, as well as the >>> operator.

Currently I'm using +> \ +< (and ~>) operators to compensate for this, but are they equivalent?

like image 266
madcrazydrumma Avatar asked Jul 15 '18 08:07

madcrazydrumma


1 Answers

It all depends what you put on the left side of the operator. Since Perl 6 works by default on bigints (aka, integer values that will grow in size until you run out of memory or get tired of waiting), it really depends on whether you have a negative value or a positive value when you right shift.

say  2**65 +> 63; #  4
say -2**65 +> 63; # -4

Consequently you can left shift as far as you want:

say  1 +< 65; #  36893488147419103232
say -1 +< 65; # -36893488147419103232

Now, if you want to limit yourself to native integers, usually 64-bit, then you get wrapping:

my int $i = 1;         say $i +< 65; # 2
my int $i = 1;         say $i +< 63; # -9223372036854775808
my int $i = 2**63 - 1; say $i +> 62; # 1

And one could argue these are then the equivalent of << and >> in Java. And that Perl 6 does not have an equivalent of Java's >>> (yet anyway).

Also, the ~> operator, although specced, is currently not yet implemented. Patches welcome!

like image 170
Elizabeth Mattijsen Avatar answered Oct 08 '22 08:10

Elizabeth Mattijsen