Skip to content

Commit

Permalink
Amend documentation of shift operators in perlop.pod.
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphus authored and khwilliamson committed Dec 18, 2024
1 parent 1e1959c commit 7496ff1
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pod/perlop.pod
Original file line number Diff line number Diff line change
Expand Up @@ -480,22 +480,17 @@ Shifting by negative number of bits means the reverse shift: left
shift becomes right shift, right shift becomes left shift. This is
unlike in C, where negative shift is undefined.

Shifting by more bits than the size of the integers means most of the
time zero (all bits fall off), except that under S<C<use integer>>
right overshifting a negative shiftee results in -1. This is unlike
in C, where shifting by too many bits is undefined. A common C
behavior is "shift by modulo wordbits", so that for example

1 >> 64 == 1 >> (64 % 64) == 1 >> 0 == 1 # Common C behavior.

but that is completely accidental.
Shifting by a value greater than or equal to integer size (in bits)
results in zero (all bits fall off), except that under S<C<use integer>>
right shifting a negative value by such an amount results in -1.
This is unlike in C, where shifting by too many bits is undefined.

If you get tired of being subject to your platform's native integers,
the S<C<use bigint>> pragma neatly sidesteps the issue altogether:

print 20 << 20; # 20971520
print 20 << 40; # 5120 on 32-bit machines,
# 21990232555520 on 64-bit machines
print 20 << 32; # 0 with 32-bit integer,
# 85899345920 with 64-bit integer
use bigint;
print 20 << 100; # 25353012004564588029934064107520

Expand Down

0 comments on commit 7496ff1

Please sign in to comment.