value Shl count
Parameters
|
value - the integer to shift count - how many bit positions to shift left by |
Description
|
Shifts an integer's bits to the left, filling the vacated low bits with zeros. Shl is an operator written between its two values: 1 Shl 4 is 16. Each place you shift left doubles the number, so value Shl n is value multiplied by 2 to the power n. It sits between + - and * / in precedence, so bracket mixed expressions if you want to be sure. Shifting is how you build bit masks and packed values. 1 Shl bit_number gives you the mask for a single flag, which you then combine with Or and test with And. Packing a colour by hand is (r Shl 16) Or (g Shl 8) Or b. Tile and chunk indexing often uses shifts too, where a power-of-two grid makes x Shl 5 stand in for x*32. Bits shifted off the top are simply lost, and shifting far enough will push your value into the sign bit and make it negative, so Shl is not a safe substitute for multiplication once numbers get large. See also: Shr, Sar, And, Or, Bin. |
Example
; Shl Example ; ----------- ; Shl shifts a value's bits to the left: each shift doubles the value. ; (Shr shifts right to halve; Sar is a right shift that keeps the ; sign.) Bin$ shows the bits so you can watch them move. value=100 Print "value = "+Bin$(value)+" ("+value+")" Print "value Shl 1 = "+Bin$(value Shl 1)+" ("+(value Shl 1)+")" Print "value Shl 3 = "+Bin$(value Shl 3)+" ("+(value Shl 3)+")" Print "" Print "value Shr 1 = "+Bin$(value Shr 1)+" ("+(value Shr 1)+")" Print "value Shr 3 = "+Bin$(value Shr 3)+" ("+(value Shr 3)+")" Print "" ; On NEGATIVE numbers Shr and Sar differ: Shr shifts zeros in from ; the left (the sign is lost), Sar copies the sign bit (stays negative) neg=-100 Print "neg = "+Bin$(neg)+" ("+neg+")" Print "neg Shr 2 = "+Bin$(neg Shr 2)+" ("+(neg Shr 2)+")" Print "neg Sar 2 = "+Bin$(neg Sar 2)+" ("+(neg Sar 2)+")" Print "" Print "Press any key to close the example" WaitKey End
Index