Unary/Bitwise NOT (!)

As we discussed in the previous section NextBASIC provides one additional unary operator, which is an operator that requires one number alone. This is:

! bitwise NOT

Bitwise NOT inverts the bits of said number from 0 to 1 and vice-versa.

PRINT %!15 returns 65520 as 15 gets inverted
to become 65520
(0000 0000 0000 1111)
(1111 1111 1111 0000)
PRINT %!43690 returns 21845 as 43690 gets inverted
to become 21845
(1010 1010 1010 1010)
(0101 0101 0101 0101)

this seems pretty straightforward correct? Wrong because look at what happens once you omit the % symbol and the expression is no longer an integer one:

PRINT !43690 returns -43691 as 43690 gets inverted together with its sign bit
to become -43691
(1010 1010 1010 1010)
(0101 0101 0101 0101)

what happened can be found in a simple phrase: two's complement which we'll examine further below. Until then however run the following examples:

10 PRINT !32767
20 PRINT %! 32767
30 PRINT % SGN{!32767}

if you feel a bit overwhelmed, not to worry; soon all will be very clear!

Bitwise operators <<, >>, &, |, ↑, ↑|

NextBASIC, can also perform 5 bitwise operations (that is operations on the individual binary digits that make up a number) on variables and expressions. These are:

x << y Shift each bit of x, y places left
x >> y Shift each bit of x, y places right
x & y Bitwise AND between x and y
x | y Bitwise OR between x and y
x ↑ y Bitwise XOR between x and y (Deprecated, use the next one)
x ↑| y Bitwise XOR between x and y (Synonym with the previous)

More information on Bitwise operations together with examples (as binary examples are much easier to understand) can be found in Integer Expressions below. The operators however work on both (except for the deprecated one) Integer and Floating Point expressions

Logical operators

Standard logical operators can be used within integer expressions if prefixed by a %. These are used in the same manner as their floating point counterparts.

x AND y Logical AND (gives 0 if y is zero, x if y is non-zero)
x OR y Logical OR (gives x if y is zero, 1 if y is non-zero)
NOT n Logical NOT (zero -> 1, non-zero -> 0)

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.