Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amend documentation of shift operators in perlop.pod. #22861

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/integer/the &/

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
Loading