Operators
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulo (remainder) | a % b |
gpc
int a = 10;
int b = 3;
int sum = a + b; // 13
int diff = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3
int remainder = a % b;// 1Comparison Operators
| Operator | Description | Example |
|---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
< |
Less than | a < b |
> |
Greater than | a > b |
<= |
Less than or equal | a <= b |
>= |
Greater than or equal | a >= b |
gpc
int x = 5;
if (x == 5) { } // true
if (x != 0) { } // true
if (x < 10) { } // true
if (x >= 5) { } // trueLogical Operators
| Operator | Description | Example |
|---|---|---|
&& |
Logical AND | a && b |
^^ |
Logical XOR | a ^^ b |
|| |
Logical OR | a || b |
! |
Logical NOT | !a |
gpc
bool a = TRUE;
bool b = FALSE;
if (a && b) { } // false
if (a ^^ b) { } // true when exactly one side is true
if (a || b) { } // true
if (!b) { } // trueShort-circuit evaluation:
gpc
// Second condition not evaluated if first is false
if (get_val(BUTTON_9) && get_val(BUTTON_10)) { }Bitwise Operators
| Operator | Description | Example |
|---|---|---|
& |
Bitwise AND | a & b |
| |
Bitwise OR | a | b |
^ |
Bitwise XOR | a ^ b |
~ |
Bitwise NOT | ~a |
<< |
Left shift | a << n |
>> |
Right shift | a >> n |
gpc
int flags = 0x0F;
int masked = flags & 0x03; // 0x03
int combined = flags | 0xF0; // 0xFF
int toggled = flags ^ 0x0F; // 0x00
int inverted = ~flags; // Bitwise inverse of the integer value
int shifted_left = flags << 2;
int shifted_right = flags >> 2;Assignment Operators
| Operator | Description | Equivalent |
|---|---|---|
= |
Assign | a = b |
+= |
Add and assign | a = a + b |
-= |
Subtract and assign | a = a - b |
*= |
Multiply and assign | a = a * b |
/= |
Divide and assign | a = a / b |
%= |
Modulo and assign | a = a % b |
&= |
AND and assign | a = a & b |
|= |
OR and assign | a = a | b |
^= |
XOR and assign | a = a ^ b |
<<= |
Left shift and assign | a = a << b |
>>= |
Right shift and assign | a = a >> b |
gpc
int x = 10;
x += 5; // x = 15
x -= 3; // x = 12
x *= 2; // x = 24
x /= 4; // x = 6Increment/Decrement
| Operator | Description | Example |
|---|---|---|
++ |
Increment by 1 | a++ or ++a |
-- |
Decrement by 1 | a-- or --a |
gpc
int count = 0;
count++; // count = 1
++count; // count = 2
count--; // count = 1The conditional expression condition ? a : b is not supported. Use an if / else block to choose a value.
Operator Precedence
From highest to lowest:
| Precedence | Operators |
|---|---|
| 1 | () [] ++ -- (postfix) |
| 2 | ! ~ - + ++ -- (prefix) |
| 3 | * / % |
| 4 | + - |
| 5 | << >> |
| 6 | < <= > >= |
| 7 | == != |
| 8 | & |
| 9 | ^ |
| 10 | | |
| 11 | && |
| 12 | ^^ |
| 13 | || |
| 14 | = += -= etc. |
Use parentheses to clarify intent:
gpc
// Clearer with parentheses
int result = (a + b) * c;
bool check = (x > 0) && (y < 100);