bit ops
This commit is contained in:
42
specs.md
42
specs.md
@@ -65,6 +65,10 @@ GLSL;
|
||||
| `>=` | greater or equal |
|
||||
| `&` | bitwise AND |
|
||||
| `\|` | bitwise OR |
|
||||
| `^` | bitwise XOR |
|
||||
| `~` | bitwise NOT (unary) |
|
||||
| `<<` | left shift |
|
||||
| `>>` | right shift (arithmetic for signed, logical for unsigned) |
|
||||
| `and` | logical AND (short-circuit) |
|
||||
| `or` | logical OR (short-circuit) |
|
||||
| `in` | membership test (tuples) |
|
||||
@@ -72,6 +76,11 @@ GLSL;
|
||||
| `-=` | sub-assign |
|
||||
| `*=` | mul-assign |
|
||||
| `/=` | div-assign |
|
||||
| `&=` | bitwise AND assign |
|
||||
| `\|=` | bitwise OR assign |
|
||||
| `^=` | bitwise XOR assign |
|
||||
| `<<=` | left shift assign |
|
||||
| `>>=` | right shift assign |
|
||||
|
||||
### Delimiters and Punctuation
|
||||
|
||||
@@ -768,11 +777,27 @@ Restrictions:
|
||||
|
||||
### Bitwise Operators
|
||||
|
||||
`&` (bitwise AND) and `|` (bitwise OR) work on all integer types, not just flags. They sit at precedence level 3, between comparisons and logical operators.
|
||||
All bitwise operators work on integer types. `>>` is arithmetic (sign-extending) for signed types and logical (zero-filling) for unsigned types.
|
||||
|
||||
```sx
|
||||
x := 0xFF & 0x0F; // 15
|
||||
y := 1 | 2 | 4; // 7
|
||||
x := 0xFF & 0x0F; // 15 — AND
|
||||
y := 1 | 2 | 4; // 7 — OR
|
||||
z := 0xFF ^ 0x0F; // 240 — XOR
|
||||
w := ~0; // -1 — NOT
|
||||
a := 1 << 4; // 16 — left shift
|
||||
b := 256 >> 4; // 16 — right shift
|
||||
```
|
||||
|
||||
Compound assignment forms: `&=`, `|=`, `^=`, `<<=`, `>>=`.
|
||||
|
||||
```sx
|
||||
x := 0xFF;
|
||||
x &= 0x0F; // 15
|
||||
x |= 0xF0; // 255
|
||||
x ^= 0x0F; // 240
|
||||
y := 1;
|
||||
y <<= 8; // 256
|
||||
y >>= 4; // 16
|
||||
```
|
||||
|
||||
---
|
||||
@@ -785,10 +810,13 @@ Everything in `sx` is expression-oriented where possible.
|
||||
|
||||
| Prec | Operators | Notes |
|
||||
|------|-----------|-------|
|
||||
| 6 (highest) | `*`, `/`, `%` | multiplication, division, modulo |
|
||||
| 5 | `+`, `-` | addition, subtraction |
|
||||
| 4 | `<`, `<=`, `>`, `>=`, `==`, `!=` | comparisons (chainable) |
|
||||
| 3 | `&`, `\|` | bitwise AND, bitwise OR |
|
||||
| 9 (highest) | `*`, `/`, `%` | multiplication, division, modulo |
|
||||
| 8 | `+`, `-` | addition, subtraction |
|
||||
| 7 | `<<`, `>>` | shifts |
|
||||
| 6 | `<`, `<=`, `>`, `>=`, `==`, `!=` | comparisons (chainable) |
|
||||
| 5 | `&` | bitwise AND |
|
||||
| 4 | `^` | bitwise XOR |
|
||||
| 3 | `\|` | bitwise OR |
|
||||
| 2 | `and` | logical AND (short-circuit) |
|
||||
| 1 (lowest) | `or` | logical OR (short-circuit) |
|
||||
|
||||
|
||||
Reference in New Issue
Block a user