Operators
Arithmetic, comparison, logical, bitwise, ternary, unary, and assignment operators in BWSL.
Pressure-test the syntax
Take the concept from this page into the playground and deliberately break a pass, binding, or type signature to see how the compiler responds.
Try a Live EditBWSL provides the standard arithmetic and control operators used across the current compiler and regression suite.
Arithmetic Operators
Arithmetic operators perform mathematical operations on numeric values:
| Operator | Name | Example | Description |
|---|---|---|---|
+ | Addition | a + b | Adds two values |
- | Subtraction | a - b | Subtracts right from left |
* | Multiplication | a * b | Multiplies two values |
/ | Division | a / b | Divides left by right |
% | Modulo | a % b | Remainder after division |
float x = 10.0 + 5.0;
float y = 10.0 - 3.0;
float z = 4.0 * 2.5;
float w = 20.0 / 4.0;
int remainder = 17 % 5;
Vector Arithmetic
Arithmetic operators work component-wise on vectors:
float3 a = float3(1.0, 2.0, 3.0);
float3 b = float3(4.0, 5.0, 6.0);
float3 sum = a + b;
float3 diff = b - a;
float3 prod = a * b;
float3 quot = b / a;
Scalar-vector operations broadcast the scalar to all components:
float3 v = float3(2.0, 4.0, 6.0);
float3 scaled = v * 0.5;
float3 offset = v + 1.0;
Comparison Operators
Comparison operators return booleans:
| Operator | Name | Example | Description |
|---|---|---|---|
< | Less than | a < b | True if left is less than right |
> | Greater than | a > b | True if left is greater than right |
<= | Less or equal | a <= b | True if left is less than or equal to right |
>= | Greater or equal | a >= b | True if left is greater than or equal to right |
== | Equal | a == b | True if values are equal |
!= | Not equal | a != b | True if values are not equal |
Logical Operators
Logical operators combine boolean expressions:
| Operator | Name | Example | Description |
|---|---|---|---|
&& | Logical AND | a && b | True if both operands are true |
|| | Logical OR | a || b | True if either operand is true |
bool inRange = (x >= 0.0) && (x <= 1.0);
bool outOfBounds = (x < 0.0) || (x > 1.0);
Logical operators use short-circuit evaluation.
Bitwise Operators
Bitwise operators work on integer and integer-backed enum values:
| Operator | Name | Example | Description |
|---|---|---|---|
& | Bitwise AND | a & b | Keeps bits set in both operands |
| | Bitwise OR | a | b | Keeps bits set in either operand |
^ | Bitwise XOR | a ^ b | Keeps bits set in exactly one operand |
~ | Bitwise NOT | ~a | Inverts all bits |
<< | Left shift | 1u << 4u | Shifts bits left |
>> | Right shift | value >> 8u | Shifts bits right |
uint flags = 0u;
flags = flags | (1u << 0u);
flags = flags | (1u << 2u);
uint mask = 0xFFu;
uint value = 0x12345678u;
uint lowByte = value & mask;
uint nextByte = (value >> 8u) & mask;
Unary Operators
Unary operators take a single operand:
| Operator | Name | Example | Description |
|---|---|---|---|
! | Logical NOT | !a | Inverts a boolean value |
- | Negation | -a | Negates a numeric value |
+ | Unary plus | +a | Returns the value unchanged |
++ | Increment | ++a or a++ | Increases value by 1 |
-- | Decrement | --a or a-- | Decreases value by 1 |
bool visible = true;
bool hidden = !visible;
float value = 5.0;
float negated = -value;
int counter = 0;
counter++;
++counter;
counter--;
Prefix vs Postfix
The prefix form (++a) increments before returning the value, while the postfix form (a++) returns the original value before incrementing:
int a = 5;
int b = ++a;
int c = 5;
int d = c++;
Ternary Operator
Use ?: for conditional selection:
float brightness = isLit ? litValue : unlitValue;
float3 color = enabled ? baseColor : float3(0.0);
The same form is also used in compile-time stage selection:
vertex = usePrimary ? primaryVertex() : fallbackVertex()
Assignment Operators
Assignment operators store values in variables:
| Operator | Name | Example | Equivalent |
|---|---|---|---|
= | Assignment | a = b | Assigns value of b to a |
+= | Add assign | a += b | a = a + b |
-= | Subtract assign | a -= b | a = a - b |
*= | Multiply assign | a *= b | a = a * b |
/= | Divide assign | a /= b | a = a / b |
float total = 0.0;
total += 10.0;
total -= 3.0;
total *= 2.0;
total /= 7.0;
Compound assignment also works with vectors:
float3 position = float3(0.0, 0.0, 0.0);
float3 velocity = float3(1.0, 0.5, 0.0);
position += velocity;
position *= 2.0;
Operator Precedence
Operators are evaluated in the following order, from highest to lowest precedence:
++--!-+++--*/%+-<<=>>===!=&&||=+=-=*=/=float result = 2.0 + 3.0 * 4.0;
bool check = x > 0.0 && y > 0.0;
When in doubt, use parentheses to make precedence explicit.
See Also
- Language Overview - BWSL syntax and concepts
- Types - Scalar and vector types
- Loops - Iteration constructs