Operators

The Propeller chip features a powerful set of math and logic operators. A subset of these operators is supported by the Propeller Assembly language; however, since the Spin language has a use for every form of operator supported by the Propeller, this section describes every operator in detail. Please see the Operators section for a list of operators available in Propeller Assembly.

Expression Workspace

The Propeller is a 32-bit device and, unless otherwise noted, expressions are always evaluated using 32-bit, signed integer math. This includes intermediate results as well. If any intermediate result overflows a 32-bit signed integer (above 2,147,483,647 or below 2,147,483,648), the final result of the expression will not be as expected. A workspace of 32 bits provides lots of room for intermediate results but it is still wise to keep overflow possibilities in mind.

If mathematical truncation is an issue, or if an expression requires real numbers rather than integers, floating-point support can help. The compiler supports 32-bit floating-point values and constant expressions with many of the same math operators as it does for integer constant expressions. Note that this is for constant expressions only, not run time variable expressions. For floating-point run-time expressions, the Propeller chip provides support through the FloatMath object supplied with the software installation. See Constant Assignment '='FLOATROUND, and TRUNC, as well as the FloatMath and FloatString objects for more information.

Operator Attributes

The operators have the following important attributes, each of which is shown in the following two tables and further explained afterwards:

  • Unary / Binary
  • Normal / Assignment
  • Constant and/or Variable Expression
  • Level of Precedence

Math and Logic Operators


Operator


Assignment Usage

Constant Expressions1


Is Unary


Description

Integer

Float

=

always

n/a1

n/a1

 

Constant Assignment '=' (CON blocks only)

:=

always

n/a1

n/a1

 

Variable Assignment ':=' (PUB/PRI blocks only)

+

+=

 

Add '+', '+='

+

never

Positive (unary form of Add) '+' (+X)

-

-=

 

Subtract '-', '-='

-

if solo

Negate (unary form of Subtract) '-' (-X)

 

always

 

 

Decrement, pre- or post- '--' ((--X) or (X--))

++

always

 

 

Decrement, pre- or post- '++' ((++X) or (X++))

*

*=

 

Multiply, Return Low '*', '*=',  multiply and return lower 32 bits (signed)

**

**=

 

 

Multiply, Return High '**', '**=', multiply and return upper 32 bits (signed)

/

/=

 

Divide '/', '/=' (signed)

//

//=

 

 

Modulus '//', '//=' (signed)

#>

#>=

 

Limit Minimum '#>', '#>=' (signed)

<#

<#=

 

Limit Maximum '<#', '<#=' (signed)

^^

if solo

Square Root '^^'

| |if solo Absolute Value '| |'

~

always

 

 

Sign-Extend 7 or Post-Clear '~', sign-extend from bit 7 (~X) or post-clear to 0 (X~); all bits low

~~

always

 

 

Sign-Extend 15 or Post-Set '~~', sign-extend from bit 15 (~~X) or post-set to -1 (X~~); all bits high

~>

~>=

 

 

Bitwise Shift Arithmetic Right '~>', '~>='

?

always

 

 

Random '?', random number forward (?X) or reverse (X?)

|<if solo Bitwise Decode '|<', decode value (0 - 31) into single-high-bit long
>|if solo Bitwise Encode '>|', encode long into value (0 - 32) as high-bit priority

<<

<<=

 

 

Bitwise Shift Left '<<', '<<='

>>

>>=

 

 

Bitwise Shift Right '>>', '>>='

<-

<-=

 

 

Bitwise Rotate Left '<-', '<-='

->

->=

 

 

Bitwise Rotate Right '->', '->='

><

><=

 

 

Bitwise Reverse '><', '><='

&

&=

 

 

Bitwise AND '&', '&='

||= Bitwise OR '|', '|='

^

^=

 

 

Bitwise XOR '^', '^='

!

if solo

 

Bitwise NOT '!'

AND

AND=

 

Boolean AND 'AND', 'AND=' (promotes non-0 to -1)

OR

OR=

 

Boolean OR 'OR', 'OR=' (promotes non-0 to -1)

NOT

if solo

Boolean NOT 'NOT' (promotes non-0 to -1)

==

===

 

Boolean Is Equal '==', '==='

<>

<>=

 

Boolean Is Not Equal '<>', '<>='

<

<=

 

Boolean Is Less Than '<', '<=' (signed)

>

>=

 

Boolean Is Greater Than '>', '>=' (signed)

=<

=<=

 

Boolean Is Equal or Less '=<', '=<=' (signed)

=>

=>=

 

Boolean Is Equal or Greater '=>', '=>=' (signed)

@

never

 

Address Symbol '@'

@@

never

 

 

Object Address Plus Symbol '@@'

1 Assignment forms of operators are not allowed in constant expressions.
.

Operator Precedence Levels

Level

Notes

Operators

Operator Names

Highest (0)Unary--, ++, ~, ~~, ?, @, @@

Inc/Decrement, Clear, Set, Random, Symbol/Object Address

1Unary+, -, ^^, | | , |<, >|, !Positive, Negate, Square Root, Absolute, Decode, Encode, Bitwise NOT

2

 

->, <-, >>, <<, ~>, ><

Rotate Right/Left, Shift Right/Left, Shift Arithmetic Right, Reverse

3

 

&

Bitwise AND

4 | , ^Bitwise OR, Bitwise XOR

5

 

*, **, /, //

Multiply-Low, Multiply-High, Divide, Modulus

6

 

+, -

Add, Subtract

7

 

#>, <#

Limit Minimum/Maximum

8

 

<, >, <>, ==, =<, =>

Boolean: Less/Greater Than, Not Equal, Equal, Equal or Less/Greater

9

Unary

NOT

Boolean NOT

10

 

AND

Boolean AND

11

 

OR

Boolean OR

Lowest (12)

 

=, :=, all other assignments

Constant/Variable Assignment, assignment forms of Binary Operators

Unary / Binary

Each operator is either unary or binary in nature. Unary operators are those that operate on only one operand. For example:

!Flag                            'bitwise NOT of Flag
^^Total                          'square root of Total 

Binary operators are those that operate on two operands. For example:

X + Y                            'add X and Y
Num << 4                         'shift Num left 4 bits 

Note that the term "binary operator" means "two operands," and has nothing to do with binary digits. To distinguish operators whose function relates to binary digits, we'll use the term "bitwise" instead.

Normal / Assignment

Normal operators, like Add '+' and Shift Left '<<', operate on their operand(s) and provide the result for use by the rest of the expression, without affecting the operand or operands themselves. Those that are assignment operators, however, write their result to either the variable they operated on (unary), or to the variable to their immediate left (binary), in addition to providing the result for use by the rest of the expression.

Here are assignment operator examples:

Count++                          '(Unary) evaluate Count + 1 
                                 'and write result to Count
Data >>= 3                       '(Binary) shift Data right 3 bits 
                                 'and write result to Data 

Binary operators have special forms that end in equal '=' to make them assignment operators. Unary operators do not have a special assignment form; some always assign while others assign only in special situations. See the Math and Logic Operators table above and the operator's explanation, for more information.

Most assignment operators can only be used within methods (PUB and PRI blocks). The only exception is the constant assignment operator '=' which can only be used in CON blocks.

Constant and/or Variable Expression

Operators which have the integer-constant-expression attribute can be used both at run time in variable expressions, and at compile time in constant expressions. Operators that have the float-constant-expression attribute can be used in compile-time constant expressions. Operators without either of the constant-expression attributes can only be used at run time in variable expressions. Most operators have a normal, non-assignment form that allows them to be used in both constant and variable expressions.

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.