Decimal Binary
33088 1000 0001 0100 0000
-32448 1000 0001 0100 0000
32447 0111 1110 1011 1111 Don't mind this for now!

Do you now see the pattern? Let's do one more thing that will illustrate how the computer stores the data internally (We'll now jump a bit ahead and borrow a bit from Chapter 24).

Type the following program:

10 DPOKE 30000, %-32448
20 PRINT % DPEEK 30000: PRINT
   PEEK 30000, PEEK 30001

Line 10 enters the entire 16 bits of the value -32448 into memory locations 30000 and 30001, while line 20 first prints what's stored in locations 30000 and 30001 as an unsigned integer and then the individual bytes that make up that value. You will get:

33088
64          129

The second line just translates to 0100 0000 and 1000 0001 in binary which if we consider that the smallest portion of the 16 bit number was stored first we can rebuild it as: (129 x 256) + 64 which equals... 33088!

Now let's first give some background so we can tie all this information together: A signed integer is one with either a plus or minus sign in front indicated by one bit in the beginning of the number. Since we have 16 bits assigned to integers and taking the one bit out for the sign, that would leave us 15 bits to display a number with a sign (whereas this sign is positive or negative). Thus a 16 bit signed integer will be able to display numbers to the range of -32768 to +32767. This obviously, also means that unsigned integers can have a value twice as high as signed integers. The most common way to represent signed numbers (and the one NextBASIC uses) is to use two's complement if you recall from earlier in the chapter which works as follows:

On any given binary number representing a decimal x, its two's complement is a binary number constituted by the first number with inverted digits from 0 to 1 and vice-versa and then adding 1. The resulting binary number represents decimal -x. For example:

For decimal number 2 (represented in 8 bit binary as 00000010), -2 would be 00000010's two's complement. To calculate it we'd have to invert the digits making it 11111101 and then add 1 which would make the resulting number 11111110. The very first bit signifies the sign (0 for positive and 1 for negative). The benefit of using two's complement is that standard arithmetic works properly and any numbers that exceed the bit-width of the numbers get discarded.

After discussing this, the pattern emerging from the previous examples becomes clear!

What happened in the examples above is that NextBASIC, in the first example (as mentioned in the beginning of this chapter) truncated the sign bit as it was located in the 17th bit and left us with only the 16 bit unsigned integers of the negative number which is the same as the 16 bit equivalent of the number we fed it. It then tried to interpret the sign bit but since regular integers are unsigned it just returned the positive integer that's represented by the number. For the computer therefore in both cases, what we fed it and what it printed were the exact same number

A further illustration of the above can be shown by using the unary not operator (!) which as we discussed earlier in the chapter, inverts the number. Let's see:

PRINT %!1088, %!33088

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.