Table of Contents

SHR

PET VIC-20 C64 C16 Plus/4 C128 X16

The SHR() function returns a number bit-shifted <n> positions to the right.

Function header

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.

  • If the number is unsigned (BYTE or WORD), the most significant bit gets filled with a zero.
  • If the number is signed (INT or LONG), signed right shifting is carried out, i. e. the most significant bit will be filled with the sign.

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.

Example

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

See also