Sign in Join
Wiki copytemporary

Shr: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11Back to [[Reserved words]].
22
33## Overview
44
55The reserved word **Sh**ift **r**ight (shr) performs a logical right bit-shift operation (opposite than [[Shl|shl]]).
66
77Example:
88
9-`Command is: 00000100 shr 2 (shift right 2 bits)`\
10-` `\
11-`Action is:  00000100 -> 00 (00 gets added to the left of the value; right 00 "disappears")`\
12-` `\
13-`Result is:  00000001`
9+```text
10+Command is: 00000100 shr 2 (shift right 2 bits)
11+
12+Action is: 00000100 -> 00 (00 gets added to the left of the value; right 00 "disappears")
13+
14+Result is: 00000001
15+```
1416
1517## Shr with signed types
1618
1719Note: unlike the \>\> operator in the C language, the shr operator is a logical (not arithmetic) bit shift, even if the left operand is a signed integer. An implicit [[Typecast|typecast]] and extension to a larger unsigned type may be performed before the shift operation. Check what the following program actually prints.
1820
1921``` pascal
2022program ShrTest;
2123begin
2224 WriteLn(ShortInt(-3) shr 1);
2325end.
2426```
2527
2628## Is a bit set
2729
2830``` pascal
2931function isBitSet(AValue, ABitNumber:integer):boolean;
3032begin
3133 result:=odd(AValue shr ABitNumber);
3234end;
3335```
3436
3537- [[Const]]
3638- [[Function]]
3739- [[Integer]]
3840
3941---
4042*From the Free Pascal wiki, page "Shr", as it stood on 2021-09-01 after a change by Mozi, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history