<!-- PDF page 45 -->

<table>
<tbody>
<tr><td>227</td><td></td><td><code>11100011</code></td></tr>
<tr><td><strong>AND</strong> 2</td><td>(Bitmask)</td><td><u><code>00000010</code></u></td></tr>
<tr><td>Result</td><td></td><td><code>00000010</code></td></tr>
</tbody>
</table>

Bit 2 of the mask, matches bit 2 of the number and it is **1** therefore **10** is returned (binary equivalent of decimal **2**)

Bitwise OR (**|**) will return **1** in any position if at least one bit of the two numbers is the same position is **1** and **0** if both are set to **0**. For example:

```
PRINT %@10101010 | @01101011
```

will return **235** as only bits in positions 3 and 5 in both numbers are set to **0** making the resulting number **11101011** in binary form (or **235** in decimal). To better illustrate:

<table>
<tbody>
<tr><td>170</td><td></td><td><code>10101010</code></td></tr>
<tr><td><strong>OR</strong> 107</td><td>(Bitmask)</td><td><u><code>01101011</code></u></td></tr>
<tr><td>Result</td><td></td><td><code>11101011</code></td></tr>
</tbody>
</table>

Finally, bitwise XOR (↑|) will only return **1** in any position if either bit is set to **1** but *not both*. So two 0s *and* two 1s, both return **0** in a position. Using the same numbers as in the previous example:

```
PRINT %@10101010 ↑| @01101011
```

will return **193** or binary **1100001** since:

<table>
<tbody>
<tr><td>170</td><td></td><td><code>10101010</code></td></tr>
<tr><td><strong>XOR</strong> 107</td><td>(Bitmask)</td><td><u><code>01101011</code></u></td></tr>
<tr><td>Result</td><td></td><td><code>11000001</code></td></tr>
</tbody>
</table>

Bitwise expressions are uniquely helpful in determining the condition of flags in several of the ZX Spectrum Next ports (as we will see in *Chapter 22*), since these take the form of individual bits in a binary number and testing those with regular arithmetic can be cumbersome and slow.

### Signed vs Unsigned Integer Expressions

As you saw in *Chapter 1* and in the introduction to this chapter, integer variables in *NextBASIC* are fixed to 16-bits wide *unsigned*, which means that they can display *only positive* integers from **0** to **65535**. To illustrate approximately what that means, try the following:

```
PRINT %-64448
```

The computer will respond with: **1088**. Keep this result in mind for a moment and then try:

```
PRINT %-32448
```

This time the computer will display the number **33088** on screen. Are you confused yet? Maybe seeing the numbers in binary will help. Let's start with the first response of **1088** and we'll work backwards.

| Decimal | Binary | |
|---|---|---|
| **1088** | **0000 0100 0100 0000** |  |
| **-64448** | **1 0000 0100 0100 0000** |  |
| **64447** | **1111 1011 1011 1111** | Don't mind this for now! |

Aha! Let's now see the second response:

