The SHR()
function returns a number bit-shifted <n> positions to the right.
DECLARE FUNCTION SHR AS BYTE (num AS BYTE, n AS BYTE) SHARED STATIC INLINE DECLARE FUNCTION SHR AS INT (num AS INT, n AS BYTE) OVERLOAD SHARED STATIC INLINE DECLARE FUNCTION SHR AS WORD (num AS WORD, n AS BYTE) OVERLOAD SHARED STATIC INLINE DECLARE FUNCTION SHR AS LONG (num AS LONG, n AS BYTE) OVERLOAD SHARED STATIC INLINE
Each bit in the number will be moved <n> positions to the right.
The least significant bit is discarded in all cases.
Note
Since SHR(number, n)
equals number / pow(2, n)
, the function is especially useful if you want to divide a number by powers of two. Bit shifting is carried out much faster than the division operation.
PRINT SHR(82, 1) : REM outputs 41 PRINT SHR(%11110000, 4) : REM outputs 15 (binary 00001111) PRINT SHR(%00000001, 1) : REM outputs 0 because the LSB is discarded