&
Bitwise and of the top two numbers: keeps only the bits that are set in both. Good for checking and clearing flags.
Facts
- Word
&- Family
- Bitwise
- Integer stack
- pops 2, pushes 1
- What it does
- Pops two numbers and pushes one with only the bits that are set in both turned on. With an empty stack it gives 0.
- Cost
- Bitwise command
- Runs
- inside a gene: in the condition (cond) and in the body (start or else)
a b & pops two numbers and leaves a number with a bit set only where that bit is set in both a and b. 12 10 & gives 8: in binary, 1100 and 1010 share only the bit worth 8.
| Stack before | After & |
|---|---|
12 10 | 8 |
7 4 | 4 |
5 2 | 0 |
It has two main uses:
- Checking a flag:
*50 4 &leaves 4 if the bit worth 4 in cell 50 is set and 0 if not. Finish it with a comparison:*50 4 & 0 !=. - Clearing a flag: with a mask made with
~,*50 4 ~ & 50 storeclears that bit and leaves the others alone.
A useful special case: x 1 & leaves 1 if x is odd and 0 if it is even. With .robage it makes a gene that runs every other cycle:
' moves forward only on cycles where its age is even
cond
*.robage 1 & 0 =
start
10 .up store
stopWith negatives it works on the two's complement: -1 has all its bits set, so x -1 & leaves x as is. With an empty stack it operates on zeros and gives
falses (not numbers) there is and.