Propeller Manual

Table Of Contents
Operators – Spin Language Reference
times where calculations on simple data result in word-sized values that should be treated as a
signed integer in the range of -32768 to +32767. When you need to perform further
calculations with those word-sized values, use the Sign-Extend 15 operator to convert the
number into the proper 32-bit signed integer form. In the above example, assume
X
represents the value -300, which in 16-bit twos-complement form is actually the value 65,236
(%11111110 11010100). The
~~X portion of the expression extends the sign bit from bit 15
all the way up to bit 31, converting the number to the proper 32-bit twos-complement form of
-300 (%11111111 11111111 11111110 11010100). Adding that sign-extended value to 50
results in -250, the intended result, whereas it would have resulted in 65,286 without the
proper sign extension.
The following is an example of the Post-Set operator form.
Y := X~~ + 2
The Post-Set operator in this example sets the variable to -1 (all bits high) after providing its
current value for the next operation. In this example if
X started out as 6, X~~ would provide
the current value for the expression (6 + 2) to be evaluated later, then would store -1 in
X.
The expression 6 + 2 is then evaluated and the result, 8, is stored into
Y. After this statement,
X equals -1 and Y equals 8.
Since Sign-Extend 15 and Post-Set are always assignment operators, the rules of Intermediate
Assignments apply to them (see page 147).
Shift Arithmetic Right ‘
~>’, ‘~>=
The Shift Arithmetic Right operator is just like the Shift Right operator except that it
maintains the sign, like a divide by 2, 4, 8, etc on a signed value. Shift Arithmetic Right can
be used in variable and integer constant expressions, but not in floating-point constant
expressions. Example:
X := Y ~> 4
The above example shifts Y right by 4 bits, maintaining the sign. If Y is -3200 (%11111111
11111111 11110011 10000000) then
-3200 ~> 4 = -200 (%11111111 11111111 11111111
00111000). If the same operation had been done with the Shift Right operator instead, the
result would have been 268,435,256 (%00001111 11111111 11111111 00111000).
Shift Arithmetic Right has an assignment form,
~>=, that uses the variable to its left as both
the first operand and the result destination. For example,
X ~>= 2 'Short form of X := X ~> 2
Page 158 · Propeller Manual v1.1