Shr: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | Back to [[Reserved words]]. | |
| 2 | 2 | ||
| 3 | 3 | ## Overview | |
| 4 | 4 | ||
| 5 | 5 | The reserved word **Sh**ift **r**ight (shr) performs a logical right bit-shift operation (opposite than [[Shl|shl]]). | |
| 6 | 6 | ||
| 7 | 7 | Example: | |
| 8 | 8 | ||
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 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 | + | ``` | |
| 14 | 16 | ||
| 15 | 17 | ## Shr with signed types | |
| 16 | 18 | ||
| 17 | 19 | Note: 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. | |
| 18 | 20 | ||
| 19 | 21 | ``` pascal | |
| 20 | 22 | program ShrTest; | |
| 21 | 23 | begin | |
| 22 | 24 | WriteLn(ShortInt(-3) shr 1); | |
| 23 | 25 | end. | |
| 24 | 26 | ``` | |
| 25 | 27 | ||
| 26 | 28 | ## Is a bit set | |
| 27 | 29 | ||
| 28 | 30 | ``` pascal | |
| 29 | 31 | function isBitSet(AValue, ABitNumber:integer):boolean; | |
| 30 | 32 | begin | |
| 31 | 33 | result:=odd(AValue shr ABitNumber); | |
| 32 | 34 | end; | |
| 33 | 35 | ``` | |
| 34 | 36 | ||
| 35 | 37 | - [[Const]] | |
| 36 | 38 | - [[Function]] | |
| 37 | 39 | - [[Integer]] | |
| 38 | 40 | ||
| 39 | 41 | --- | |
| 40 | 42 | *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).* |
