Bitwise
The operators that look at a number as 32 bits: and, or, exclusive or, inversion, shifts, plus one and minus one, and the sign change.
Cost: Bitwise command
| Operator | What it does |
|---|---|
~ | Inverts all the bits of the number on top: x ~ leaves −x − 1, so 5 ~ gives −6 and 0 ~ gives −1. |
& | Bitwise and of the top two numbers: keeps only the bits that are set in both. Good for checking and clearing flags. |
| | Bitwise or of the top two numbers: leaves set every bit that is set in either one. Good for setting flags. |
^ | Bitwise exclusive or of the top two numbers: leaves a number whose set bits are those in only one of the two. Good for flipping flags. |
++ | Adds 1 to the number on top of the integer stack, without touching memory: 5 ++ leaves 6. |
-- | Subtracts 1 from the number on top of the integer stack, without touching memory: 5 -- leaves 4 and 0 -- leaves −1. |
- | A loose minus sign flips the sign of the top number: 5 - leaves −5. It doesn't subtract; for that there is sub. |
<< | Shifts the bits of the top number one place to the left, that is, multiplies it by 2: 5 << leaves 10. |
>> | Shifts the bits of the top number one place to the right, keeping the sign: it divides by 2 rounding down, so 7 >> leaves 3 and −5 >> leaves −3. |
These operators work, like the basic ones, only with the integer stack: they pop one or two numbers and leave one. The difference is that they see each number as a row of 32 bits (in two's complement, the usual way of storing negatives) and operate bit by bit.
- Combining two numbers:
&(and),|(or) and^(exclusive or). - Transforming one:
~inverts all the bits,<<and>>shift them one place (doubling or halving),++and--add or subtract 1. - The loose minus sign,
-, is also part of this family even though it does not touch bits: it changes the sign of the top. It does not subtract; for subtracting there issub.
The most common use is keeping several yes-or-no flags in a single memory cell, one per bit: | with a power of 2 sets a bit, ~ and & clear it, and & alone asks whether it is set. A cell holds up to ±32000, so 14 flags fit without trouble (the bits worth 1, 2, 4… up to 8192). The one worth 16384 fits only if the sum does not go past 32000.
This bot has a 1 in cell 50 (the bit worth 1). When its age reaches 5 it sets the one worth 4 and the cell becomes 5; at 10 it clears it and goes back to 1, without touching the other bit:
' sets the bit worth 4 in cell 50 and then clears it
cond
*.robage 5 =
start
*50 4 | 50 store
stop
cond
*.robage 10 =
start
*50 4 ~ & 50 store
stopTo ask about the flag, *50 4 & 0 != leaves true if the bit is set (see !=).
None of them fails with an empty stack: they operate on zeros. At the edge of 32 bits there are two quirks inherited from DarwinBots 2.48.32 (a result that should be the most negative number, −2147483648, comes out as 0); ++ and << describe them. With numbers the size of memory you will never get there.
Every operator of this family that is executed charges the cost Bitwise command from the scenario configuration. The overview of all the families is in Operators by family.