Positron Compiler Documentation

Bitwise Shift Right '>>'

Positron16 · Revision 1.1.3.5 · PDF page 87

Assignment Variable = Variable >> Shift_Amount

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.

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.

' Unsigned Right Shift
  Device = 24FJ64GA002           ' Select the device to compile for
  Declare Xtal = 16      ' Tell the compiler the device will be operating at 16MHz
  Declare Hserial_Baud = 9600    ' USART1 Baud rate
  Declare Hrsout1_Pin = PORTB.14 ' Select the pin for TX with USART1

  Dim MyWord as Word
  Dim bBitCount as Byte
  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX

  MyWord = %1111111111111111
  For bBitCount = 1 to 16             ' Repeat with bBitCount = 1 to 16.
     HrsoutLn Bin MyWord >> bLoop    ' Shift MyWord right bBitCount places.
  Next
' Signed Right Shift
  Device = 24FJ64GA002           ' Select the device to compile for
  Declare Xtal = 16      ' Tell the compiler the device will be operating at 16MHz
  Declare Hserial_Baud = 9600    ' USART1 Baud rate
  Declare Hrsout1_Pin = PORTB.14 ' Select the pin for TX with USART1

  Dim SWordin as Sword             ' Create a signed 16-bit variable
  Dim bBitCount as Byte
  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX

  SWordin = 0b1000000000000000      ' Load SWordin with the value -32768
  For bBitCount = 0 to 15           ' Repeat with bBitCount = 0 to 15
     HRsoutLn Bin SWordin >> bBitCount ' Shift SWordin right bBitCount places
  Next

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.

View original PDF page 87Open page in PDFOriginal PDF page 87