Shl: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Shl
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Back to [[Reserved words]]. | |
| 2 | + | ||
| 3 | + | ## Overview | |
| 4 | + | ||
| 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 | + | ||
| 7 | + | Example: | |
| 8 | + | ||
| 9 | + | `Command is: 00000100 shl 2 (shift left 2 bits)`\ | |
| 10 | + | ` `\ | |
| 11 | + | `Action is: 00000100 <- 00 (00 gets added to the right of the value; left 00 "disappears")`\ | |
| 12 | + | ` `\ | |
| 13 | + | `Result is: 00010000` | |
| 14 | + | ||
| 15 | + | ## Clear a bit | |
| 16 | + | ||
| 17 | + | ``` pascal | |
| 18 | + | function ClearBit( const aValue, aBitNumber : integer ) : integer; | |
| 19 | + | begin | |
| 20 | + | // sanity check supplied value | |
| 21 | + | if (aBitNumber <0) or (aBitNumber >15) then | |
| 22 | + | result :=0 | |
| 23 | + | else | |
| 24 | + | // value ok | |
| 25 | + | result := aValue and not( 1 shl aBitNumber ); | |
| 26 | + | end; | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | If you call ClearBit(%1111,1), then you get %1101 (The [[Binary numeral system|binary number]] %1111 is 15 and %1101 = 13). | |
| 30 | + | ||
| 31 | + | If you call ClearBit(13,2), then you get 9 (9 = %1001). | |
| 32 | + | ||
| 33 | + | 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 | + | ||
| 35 | + | - [[Or|Set a bit]] | |
| 36 | + | - [[Xor|Toggle a bit]] | |
| 37 | + | - [[Bit manipulation]] | |
| 38 | + | - [[$Bitpacking]] | |
| 39 | + | ||
| 40 | + | --- | |
| 41 | + | *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 | + | Category: Code | |
| 43 | + | Category: Operators |
