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