Syntax:
Shift variable [,] { Left | Right }Command Availability:
Available on PIC and AVR microcontrollers.
Explanation:
The Shift command performs a logical shift of variable by one bit in the direction given. variable can be a Byte, Word or Long and is changed in place.
- A
0is always shifted into the vacated bit: the least significant bit onLeft, the most significant bit onRight. - The bit shifted out is discarded. Unlike Rotate,
Shiftdoes not feed the carry bit (STATUS.C) back into the variable, and the carry flag should not be relied on after the command. - A
Leftshift multiplies an unsigned value by 2, and aRightshift divides it by 2. - For a Word or Long the shift runs through every byte, so the bit shifted out of one byte is shifted into the next.
LeftandRightcan be writtenLandR. The comma, and brackets round the variable, are optional:Shift Value Left,Shift Value, LeftandShift (Value) Leftare the same.
Shift moves one bit each time it is used. To shift by several places, repeat it:
Repeat 4
Shift Value, Left
End RepeatShift is faster than FnLSL and FnLSR for a shift of one place or a few places, because it is compiled in line and does not call a function. Use FnLSL or FnLSR when the number of places is held in a variable and the shifted value is needed inside an expression.
Example:
' Shift a byte and a word, left then right.
' Look at the values in the simulator, or send them to a terminal.
#chip 16F18855, 32
Dim TestByte As Byte
Dim TestWord As Word
TestByte = 0b10010010
Shift TestByte, Left ' <<< the Shift instruction: 0b00100100, the top bit is lost
Shift TestByte, Right ' 0b00010010, a 0 enters at the top
TestWord = 0x0080
Shift TestWord, Left ' 0x0100: the bit moves from the low byte to the high byte
Shift TestWord, Right ' 0x0080
EndKey line: Shift TestWord, Left — moves every bit of the Word one place left through both bytes, a 0 entering at the bottom, with no carry fed back.
See Also:
- Rotate — rotates a variable through the carry bit instead of shifting in a 0
- FnLSL — a left shift by any number of places, as a function
- FnLSR — a right shift by any number of places, as a function
- Bitwise Operations Overview — background on GCBASIC’s bitwise operations

