Unary Operators

  -  (Negate)

Variable = -Value

Places the negative (two's compliment) value of Value (which may be a constant or variable) into Variable.

Start:
  TRIS_B = %00000000

Main:
  RB = $0F                                      ' RB = %00001111
  RB = -RB                                      ' RB = %11110001
  END


  ~NOT   (Bitwise NOT)

Variable = ~Value
Variable = NOT Value

Places the bitwise inversion of Value (which may be a constant or variable) into Variable. Each result bit is subject to the following logic:

    NOT 0 = 1
    NOT 1 = 0

Start:
  TRIS_B = %00000000

Main:
  xx = $F0                                      ' xx = %11110000
  RB = NOT xx                                   ' RB = %00001111
  RB = NOT RB                                   ' RB = %11110000
  RB = ~RB                                      ' RB = %00001111
  RB.0 = ~RB.0                                  ' RB = %00001110
  RB.1 = NOT xx.7                               ' RB = %00001100
  END


Binary Operators

  +   (Addition)

Variable = Value1 + Value2

Adds two values (variables or constants) and places result in Variable. Note that when Variable is a byte the result will be truncated to eight bits.

Start:
  TRIS_B = %00000000

Main:
  RB = $0F                                      ' RB = %00001111
  RB = RB + $F0                                 ' RB = %11111111, C = 0
  RB = RB + 2                                   ' RB = %00000001, C = 1
  END


  -   (Subtraction)

Variable = Value1 - Value2

Subtracts Value2  from Value1 and places the result in Variable. If the result is less than zero, two's-compliment format is used to store the result.

Start:
  TRIS_B = %00000000

Main:
  RB = $0F                                      ' RB = %00001111 (15)
  RB = RB - 7                                   ' RB = %00001000 (8), C = 1
  RB = RB - 10                                  ' RB = %11111110 (-2), C = 0
  END


  *   (Multiplication)

Variable = Value1 * Value2

Multiplies Value1  by Value2  and places the result in Variable.

Start:
  TRIS_B = %00000000

Main:
  RB = %00001111                                ' RB = %00001111 (15)
  RB = RB * 16                                  ' RB = %11110000 (240)
  RB = RB * 2                                   ' RB = %11100000 (224)
  END


  /   (Division)

Variable = Value1 / Value2

Divides Value1  by Value2  and places the (integer) result in Variable.

Start:
  TRIS_B = %00000000

Main:
  RB = $F0                                      ' RB = %11110000 (240)
  RB = RB / 16                                  ' RB = %00001111 (15)
  RB = RB / 4                                   ' RB = %00000011 (3)
  END


  //   (Modulus)

Variable = Value1 // Value2

Divides Value1  by Value2  and places the remainder in Variable.

Start:
  TRIS_B = %00000000

Main:
  RB = 25                                       ' RB = %00011001 (25)
  RB = RB // 10                                 ' RB = %00000101 (5)
  END

Note: The remainder of a division (modulus) is available immediately after the division or modulus operation. This code:

  dig10 = value / 10
  dig01 = __REMAINDER

... uses half the instruction space of:

  dig10 = value / 10
  dig01 = value // 10

The only requirement for using __REMAINDER (or __WREMAINDER for word values) is that the assignment to another variable must be the first instruction following the division or modulus operation.



  */   (Multiply Middle)

WordVar = Value1 */ Value2

Multiplies Value1  by Value2 , returning the middle 16 bits of the 32-bit result to WordVar. This has the effect of multiplying a value by a whole number and a fraction. The whole number is the upper byte of the multiplier (0 to 255 whole units) and the fraction is the lower byte of the multiplier (0 to 255 units of 1/256 each).

The */ (star-slash) operator gives you an excellent workaround for SX/B's integer-only math. Suppose you want to multiply a value by 1.5. The whole number, and therefore the upper byte of the multiplier, would be 1, and the lower byte (fractional part) would be 128, since 128/256 = 0.5. It may be clearer to express the */ multiplier in hex -- as $0180 -- since hex notation keeps the contents of the upper and lower bytes separate.

WATCH tmpW1

Start:
  tmpW1 = 100
  tmpW1 = tmpW1 */ $0180                        ' Multiply by 1.5 [1 + (128/256)]
  BREAK                                         ' view in Debug Watch window
  END


  **   (Multiply High)

WordVar = Value1 ** Value2

Multiplies Value1  by Value2 , returning the high 16 bits of the result to WordVar. When you multiply two 16-bit values, the result can be as large as 32 bits. Since the largest variable supported by SX/B is 16 bits, the highest 16 bits of a 32-bit multiplication result are normally lost. The ** (star-star) operator gives you these upper 16 bits.

For example, suppose you multiply 65000 ($FDE8) by itself. The result is 4,225,000,000 or $FBD46240. The * (multiplication) operator would return the lower 16 bits, $6240; the ** operator returns $FBD4.

WATCH tmpW1, 16, UHEX
WATCH tmpW2, 16, UHEX

Start:
  tmpW1 = $FDE8
  tmpW2 = tmpW1 ** tmpW1                        ' Multiply value1 by itself
  BREAK                                         ' view in Debug Watch window
  END


  MAX

Variable = Value MAX Limit

Places Value into Variable limiting the maximum value of Variable to Limit.

Start:
  TRIS_B = %00000000

Main:
  FOR idx = 0 TO 15                             ' idx = 0 .. 15
    RB = idx MAX 7                              ' RB = 0 .. 7
    PAUSE 100
  NEXT idx
  GOTO Main


  MIN

Variable = Value MIN Limit

Places Value into Variable forcing the minimum value of Variable to Limit.

Start:
  TRIS_B = %00000000

Main:
  FOR idx = 0 TO 15                             ' idx = 0 .. 15
    RB = idx MIN 7                              ' RB = 7 .. 15
    PAUSE 100
  NEXT idx
  GOTO Main


  &AND   (Bitwise AND)

Variable = Value1 & Value2
Variable = Value1 AND Value2

Performs a bitwise AND operation on Value1 and Value2, then places the result in Variable. Each result bit is subject to the following logic:

    0 & 0 = 0
    0 & 1 = 0
    1 & 0 = 0
    1 & 1 = 1

Start:
  TRIS_B = %00000000

Main:
  RB = $0F & %11111111                          ' RB = %00001111
  RB = RB AND %11111100                         ' RB = %00001100
  END


  |OR   (Bitwise OR)

Variable = Value1 | Value2
Variable = Value1 OR Value2

Performs a bitwise OR operation on Value1 and Value2, then places the result in Variable. Each result bit is subject to the following logic:

    0 | 0 = 0
    0 | 1 = 1
    1 | 0 = 1
    1 | 1 = 1

Start:
  TRIS_B = %00000000

Main:
  RB = %00001111                                ' RB = %00001111
  RB = RB | %11000000                           ' RB = %11001111
  RB = RB OR %00011111                          ' RB = %11011111
  END


  ^XOR   (Bitwise Exclusive OR)

Variable = Value1 ^ Value2
Variable = Value1 XOR Value2

Performs a bitwise Exclusive OR operation on Value1 and Value2, then places the result in Variable. Each result bit is subject to the following logic:

    0 ^ 0 = 0
    0 ^ 1 = 1
    1 ^ 0 = 1
    1 ^ 1 = 0

Start:
  TRIS_B = %00000000

Main:
  RB = %00001111                                ' RB = %00001111
  RB = RB ^ %11000000                           ' RB = %11001111
  RB = RB XOR %00011111                         ' RB = %11010000
  END


  <<SHL   (Shift Left)

Variable = Value1 << Value2
Variable = Value1 SHL Value2

Left shifts the bits of Value1 by the number specified in Value2. Bits shifted off the left end (MSB) of a number are lost; bits shifted into the right end of the number are zeros. Shifting the bits of a value left n number of times has the same effect as multiplying that number by 2 to the nth power. For instance 15 << 3 (shift the bits of the decimal number 15 left three places) is equivalent to 15 * 23 (15 * 8).

Start:
  TRIS_B = %00000000

Main:
  RB = %00001101 << 2                           ' RB = %00110100
  RB = RB SHL 4                                 ' RB = %01000000
  END


  >>SHR   (Shift Right)

Variable = Value1 >> Value2
Variable = Value1 SHR Value2

Right shifts the bits of Value1 by the number specified in Value2. Bits shifted off the right end (LSB) of a number are lost; bits shifted into the left end of the number are zeros. Shifting the bits of a value right n number of times has the same effect as dividing that number by 2 to the nth power. For instance $F0 >> 3 (shift the bits of the decimal number 240 right three places) is equivalent to 240 / 23 (240 / 8).

Start:
  TRIS_B = %00000000

Main:
  RB = %10110000 >> 2                           ' RB = %00101100
  RB = RB SHR 4                                 ' RB = %00000010
  END