Syntax:
BitsOut = FnLSR(BitsIn, NumBits)
Command Availability:
Available on all microcontrollers.
Explanation:
FnLSR (Logical Shift Right) performs a bitwise right shift. It returns BitsIn shifted right by NumBits places, equivalent to the C operation:
BitsOut = BitsIn >> NumBits
Each right shift is equivalent to dividing BitsIn by 2 (discarding any remainder). BitsIn and NumBits can each be a variable, constant, or another function’s result, of type Bit, Byte, Word, or Long. Zeros are shifted in from
the left; bits shifted out of the right end are discarded and lost — there is no rounding, so shifting an odd number right
always truncates rather than rounds.
It is useful for mathematical and logical operations, as well as for creating serial data streams or manipulating I/O ports one bit-pattern at a time.
Example:
' This program will shift the LEDs on the Microchip PIC Low Pin Count Demo Board
' from Right to Left, that is DS4(RC3) to DS1(RC0) and repeat.
#chip 16f690 ' declare the target Device
#define LEDPORT PORTC ' LEDs on pins 16, 15, 14 and 7
Dim LEDMask as Byte ' Pattern to be displayed
LEDMask = 0b1000 ' Initialise the Patten
Dir LEDPORT Out ' Enable the LED Port.
Do
LEDPORT = LEDMask ' Display the Pattern
wait 500 ms
LEDMask = FnLSR(LEDMask, 1) & 0x0F ' <<< the FnLSR instruction
if LEDPORT.0 then LEDMask.3 = 1 ' Restart the sequence
Loop
EndKey line: FnLSR(LEDMask, 1) & 0x0F — shifts the lit LED one position right, then masks the result down to 4 bits so the pattern wraps within `LEDPORT’s lower
nibble instead of drifting into unrelated bits.
See Also:
- Bitwise Operations Overview — background on GCBASIC’s bitwise operations
- FnLSL — the equivalent left-shift function
- Conditions — the
iftest used to restart the sequence in the example above

