|
| 1 | +#Bitwise Operators |
| 2 | + |
| 3 | +ZX Basic allows Bit Manipulation (bitwise), on every integer type (from 8 to 32 bits). |
| 4 | + |
| 5 | +| **BITWISE OPERATORS** | |
| 6 | +|: ---------------------------- :| |
| 7 | +| bAND | |
| 8 | +| bOR | |
| 9 | +| bNOT | |
| 10 | +| bXOR | |
| 11 | + |
| 12 | +Except bNOT, all the others require two integral (Byte, Ubyte, Integer, UInteger, Long, ULong) operands. |
| 13 | +The operation will be applied bit by bit. |
| 14 | + |
| 15 | +--- |
| 16 | +##bAND |
| 17 | + |
| 18 | +Performs the _Bitwise Conjunction_ and returns 1 for every bit if and only if both bits are 1. |
| 19 | + |
| 20 | +| a | b | result | |
| 21 | +|:----:|:----:|:------:| |
| 22 | +| 0 | 0 | 0 | |
| 23 | +| 0 | 1 | 0 | |
| 24 | +| 1 | 0 | 0 | |
| 25 | +| 1 | 1 | 1 | |
| 26 | + |
| 27 | +###Example |
| 28 | + |
| 29 | +Binary "mask" that will get only the 4 rightmost bits 0 1 2 3 of a number: |
| 30 | + |
| 31 | +`PRINT BIN 01110111 bAND BIN 00001111` will print 3, which is 0111` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## bOR |
| 36 | + |
| 37 | +Performs the _Bitwise Disjunction_ and returns 1 if any of the arguments is 1. |
| 38 | + |
| 39 | +| a | b | result | |
| 40 | +|:----:|:----:|:------:| |
| 41 | +| 0 | 0 | 0 | |
| 42 | +| 0 | 1 | 1 | |
| 43 | +| 1 | 0 | 1 | |
| 44 | +| 1 | 1 | 1 | |
| 45 | + |
| 46 | +###Example |
| 47 | + |
| 48 | +Ensure an ASCII letter is always in lowercase: |
| 49 | + |
| 50 | +`PRINT CHR$(CODE "A" OR BIN 10000)` will print `a` because lowercase letters have bit 5 set. |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## bNOT |
| 55 | + |
| 56 | +Performs the _Bitwise Negation_ and returns _1_ if the arguments is _0_ and vice versa. |
| 57 | +Basically it flips all the bits in an integer number. |
| 58 | + |
| 59 | +| a |result | |
| 60 | +|:----:|:------:| |
| 61 | +| 0 | 1 | |
| 62 | +| 1 | 0 | |
| 63 | + |
| 64 | + |
| 65 | +###Example |
| 66 | + |
| 67 | +Invert the first cell (upper-leftmost) in the screen: |
| 68 | + |
| 69 | +``` |
| 70 | +PRINT AT 0, 0; "A"; |
| 71 | +FOR i = 0 TO 3 |
| 72 | + POKE 16384 + 256 * i, bNOT PEEK(16384 + 256 * i) |
| 73 | +NEXT |
| 74 | +``` |
| 75 | +--- |
| 76 | + |
| 77 | +#bXOR |
| 78 | + |
| 79 | +Performs a logical XOR and returns 1 if one and only one of the arguments is 1, 0 if both bits are the same. |
| 80 | +In essence, returns 1 ONLY if one of the arguments is 1. |
| 81 | + |
| 82 | +| a | b | result | |
| 83 | +|:----:|:----:|:------:| |
| 84 | +| 0 | 0 | 0 | |
| 85 | +| 0 | 1 | 1 | |
| 86 | +| 1 | 0 | 1 | |
| 87 | +| 1 | 1 | 0 | |
| 88 | +--- |
| 89 | + |
| 90 | +###Example |
| 91 | + |
| 92 | +Flips an ASCII letter from lower to uppercase and vice versa |
| 93 | + |
| 94 | +`PRINT CHR$(CODE "A" bXOR BIN 10000)` |
0 commit comments