Bitwise Shift Right '>>'
Syntax
Assignment Variable = Variable >> Shift_Amount
Overview
Shifts the bits of a signed or unsigned integer variable to the right a specified number of places. Bits shifted off the right end of a number are lost, while bits shifted into the left end of the number are 0s, unless a signed variable is being shifted and the variable holds a negative value. Shifting the bits of a value right n number of times also has the effect of signed or unsigned dividing that number by two to the nth power.
For example 100 >> 3 (shift the bits of the decimal number 100 right three places) is equivalent to 100 / 2 ^ 3.
Operands
Variable can be a constant, variable or expression that holds the value to shift. Shift_Amount can be a constant, variable or expression that holds the amount of shifts to perform. Assignment Variable can be any valid variable type.
Example 1
' Unsigned Right Shift
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsoutLn
Dim Wordin as Word ' Create an unsigned 16-bit variable
Dim bBitCount as Byte
Wordin = 0b1111111111111111
For bBitCount = 0 to 15 ' Repeat with bMyLoop = 0 to 15
HRsoutLn Bin Wordin >> bBitCount ' Shift Wordin right bBitCount places
Next
Example 2
' Signed Right Shift
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsoutLn
Dim SWordin as Sword ' Create a signed 16-bit variable
Dim bBitCount as Byte
SWordin = 0b1000000000000000 ' Load SWordin with the value -32768
For bBitCount = 0 to 15 ' Repeat with bMyLoop = 0 to 15
HRsoutLn Bin SWordin >> bBitCount ' Shift SWordin right bBitCount places
Next
Note.
Bitwise operations are not permissible with floating point constants or variables.
Right bit shifts are signed or unsigned, depending on the variable type used, or if a right shift is used within an expression that has a signed assignment.
