Bitwise Operators
ZX Basic allows Bit Manipulation (bitwise), on every integer type (from 8 to 32 bits).
| BITWISE OPERATORS |
|---|
| bAND |
| bOR |
| bNOT |
| bXOR |
Except bNOT, all the others require two integral (Byte, Ubyte, Integer, UInteger, Long, ULong) operands. The operation will be applied bit by bit.
bAND
Performs the Bitwise Conjunction and returns 1 for every bit if and only if both bits are 1.
| a | b | result |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Example
Binary "mask" that will get only the 4 rightmost bits 0 1 2 3 of a number:
bOR
Performs the Bitwise Disjunction and returns 1 if any of the arguments is 1.
| a | b | result |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Example
Ensure an ASCII letter is always in lowercase:
a because lowercase letters have bit 5 set.
bNOT
Performs the Bitwise Negation and returns 1 if the arguments is 0 and vice versa. Basically it flips all the bits in an integer number.
| a | result |
|---|---|
| 0 | 1 |
| 1 | 0 |
Example
Invert the first cell (upper-leftmost) in the screen:
bXOR
Performs a logical XOR and returns 1 if one and only one of the arguments is 1, 0 if both bits are the same. In essence, returns 1 ONLY if one of the arguments is 1.
| a | b | result |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| --- |
Example
Flips an ASCII letter from lower to uppercase and vice versa