Shl: 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 **l**eft (**shl**) performs a left bit-shift operation, shifting the value by the amount of bits specified as an argument (opposite of [[Shr|shr]]). | |
| 6 | 6 | ||
| 7 | 7 | Example: | |
| 8 | 8 | ||
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 9 | + | ```text | |
| 10 | + | Command is: 00000100 shl 2 (shift left 2 bits) | |
| 11 | + | ||
| 12 | + | Action is: 00000100 <- 00 (00 gets added to the right of the value; left 00 "disappears") | |
| 13 | + | ||
| 14 | + | Result is: 00010000 | |
| 15 | + | ``` | |
| 14 | 16 | ||
| 15 | 17 | ## Clear a bit | |
| 16 | 18 | ||
| 17 | 19 | ``` pascal | |
| 18 | 20 | function ClearBit( const aValue, aBitNumber : integer ) : integer; | |
| 19 | 21 | begin | |
| 20 | 22 | // sanity check supplied value | |
| 21 | 23 | if (aBitNumber <0) or (aBitNumber >15) then | |
| 22 | 24 | result :=0 | |
| 23 | 25 | else | |
| 24 | 26 | // value ok | |
| 25 | 27 | result := aValue and not( 1 shl aBitNumber ); | |
| 26 | 28 | end; | |
| 27 | 29 | ``` | |
| 28 | 30 | ||
| 29 | 31 | If you call ClearBit(%1111,1), then you get %1101 (The [[Binary numeral system|binary number]] %1111 is 15 and %1101 = 13). | |
| 30 | 32 | ||
| 31 | 33 | If you call ClearBit(13,2), then you get 9 (9 = %1001). | |
| 32 | 34 | ||
| 33 | 35 | In this case, bits are numbered right to left from 0 to 15, bit 0 being the ones bit and bit 15 being the sign bit. | |
| 34 | 36 | ||
| 35 | 37 | - [[Or|Set a bit]] | |
| 36 | 38 | - [[Xor|Toggle a bit]] | |
| 37 | 39 | - [[Bit manipulation]] | |
| 38 | 40 | - [[$Bitpacking]] | |
| 39 | 41 | ||
| 40 | 42 | --- | |
| 41 | 43 | *From the Free Pascal wiki, page "Shl", as it stood on 2021-08-29 after a change by AndreasFilsinger, 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).* | |
| 42 | 44 | Category: Code | |
| 43 | 45 | Category: Operators |
