22711100011
AND 2(Bitmask)00000010
Result00000010

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:

17010101010
OR 107(Bitmask)01101011
Result11101011

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:

17010101010
XOR 107(Bitmask)01101011
Result11000001

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:


ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.