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?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With