value Shr count
Parameters
|
value - the integer to shift count - how many bit positions to shift right by |
Description
|
Shifts an integer's bits to the right, filling the vacated high bits with zeros. Shr is an operator written between its two values: 16 Shr 2 is 4. It treats the value as an unsigned 32-bit pattern, which is the important difference from Sar. For positive numbers, shifting right once halves the value, twice quarters it, and so on. For negative numbers it does nothing of the sort: the sign bit is a bit like any other, so -8 Shr 1 comes out as 2147483644, not -4. If you want a right shift that behaves like division on negative numbers, use Sar, which copies the sign bit down instead of feeding in zeros. Shr is the right choice when you are pulling fields out of a packed value rather than doing arithmetic - unpacking a colour with (rgb Shr 16) And 255, reading a byte out of a word, or walking a flags mask one bit at a time. See also: Sar, Shl, And, Hex, Bin. |
Example
; Shr Example ; ----------- ; Shr shifts a value's bits to the right: each shift halves the value, ; with zeros arriving from the left. (Shl shifts left to double; Sar ; is the sign-keeping right shift.) Bin$ shows the bits moving. value=100 Print "value = "+Bin$(value)+" ("+value+")" Print "value Shr 1 = "+Bin$(value Shr 1)+" ("+(value Shr 1)+")" Print "value Shr 3 = "+Bin$(value Shr 3)+" ("+(value Shr 3)+")" Print "" Print "value Shl 1 = "+Bin$(value Shl 1)+" ("+(value Shl 1)+")" Print "value Shl 3 = "+Bin$(value Shl 3)+" ("+(value Shl 3)+")" Print "" ; On NEGATIVE numbers beware: Shr shifts zeros in from the left, so ; the sign bit is lost and the result is a huge positive number. ; Use Sar when you want a right shift that 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