<!-- PDF page 44 -->

Once we see how the numbers are stored in memory as a series of bits we can easily understand what happened:

![Fig. 4 - Bit shifting](/documentation/manual/rev3/figures/p044-fig04-bit-shifting.png)

<table>
<tbody>
<tr><td></td><td></td><td></td><td></td><td>MSB</td><td colspan="14"></td><td>LSB</td><td></td><td></td><td></td></tr>
<tr><th>(1201)</th><td></td><td></td><td></td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>1</td><td>0</td><td>1</td><td>1</td><td>0</td><td>0</td><td>0</td><td>1</td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td>MSB</td><td colspan="14"></td><td>LSB</td><td>⬀</td><td>⬀</td><td>⬀</td></tr>
<tr><td></td><td></td><td></td><td></td><td>[colour: pale green] 0</td><td>[colour: pale green] 0</td><td>[colour: pale green] 0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>1</td><td>0</td><td>1</td><td>1</td><td>0</td><td>0</td><td>0</td><td>1</td></tr>
<tr><td></td><td></td><td></td><td></td><td>⇨</td><td>⇨</td><td>⇨</td><td colspan="13">Shift Right 3 bits, 3 rightmost bits disappear</td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td>MSB</td><td colspan="14"></td><td>LSB</td><td></td><td></td><td></td></tr>
<tr><th>(150)</th><td></td><td></td><td></td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>1</td><td>0</td><td>1</td><td>1</td><td>0</td><td></td><td></td><td></td></tr>
<tr><td></td><td>⬁</td><td>⬁</td><td>⬁</td><td>MSB</td><td colspan="14"></td><td>LSB</td><td></td><td></td><td></td></tr>
<tr><td></td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>1</td><td>0</td><td>1</td><td>1</td><td>0</td><td>[colour: pale green] 0</td><td>[colour: pale green] 0</td><td>[colour: pale green] 0</td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td colspan="13">Shift Left 3 bits, 3 leftmost bits disappear</td><td>⇦</td><td>⇦</td><td>⇦</td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td>MSB</td><td colspan="14"></td><td>LSB</td><td></td><td></td><td></td></tr>
<tr><th>(1200)</th><td></td><td></td><td></td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>1</td><td>0</td><td>1</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td></td><td></td><td></td></tr>
</tbody>
</table>

*Fig. 4 - Bit shifting*

What happens however if we do the same to a floating point variable? Let's rewrite the above example:

```
100 A=1201
110 A>>=3
120 A<<=3
130 PRINT A
```

which prints the same number as what we have assigned in line 100! Why the difference? We will need to recruit a *function* from a little further down this manual to help us better understand. Just type the following:

```
100 A=1201: PRINT
    A,STR$(A,2,4)
110 A>>=3: PRINT A,STR$(A,2,4)
120 A<<=3:PRINT A,STR$(A,2,4)
```

The whole trick is in the fractional part of the number we don't normally see because it's **0**. By shifting 3 places to the right, we occupied 3 of the fractional places and therefore when we shifted back to the left the number wasn't truncated from the right side of its binary representation!

The remaining bitwise operations are very straightforward. Bitwise AND (**&**) is used to quickly determine if a bit inside a number is set to **1** or not. The first operand is the number we want to check and the second one is called the *bitmask* which is the number we check against. Consider these two examples:

```
PRINT %@10101010 & @01010101
PRINT @11100011 & @10
```

First example will return **0** while the second **2**. The reason for this, is that the numbers in the first example don't have coinciding **1** bits in the same positions while on the second example the second bit will be **1** and as a consequence the bits that match will be the first and second which make binary **10** which in decimal equals **2**. To illustrate further:

<table>
<tbody>
<tr><td>170</td><td></td><td><code>10101010</code></td></tr>
<tr><td><strong>AND</strong> 85</td><td>(Bitmask)</td><td><u><code>01010101</code></u></td></tr>
<tr><td>Result</td><td></td><td><code>00000000</code></td></tr>
</tbody>
</table>

As you can see, no bit set to **1** in any position of the two numbers matches each other, therefore the result returned is **0** whereas in the second example:

