Short answer
Why -1 and not 1?
It's just a convention
Some speculation...
The reason underlying the choice for this convention may be related to the internal representation of integers numbers on most platforms:
Suppose a boolean value is stored in a 16 bits wide integer; false
is 0
(every bit unset).
0 => 00000000 00000000
It makes sense to agree upon another convention where true
is all bits set:
11111111 11111111
(a very reasonable choice since all 1
is the bitwise NOT
of all 0
)
The decimal representation of a signed integer whose bits are all set is -1
-1 => 11111111 11111111
While the binary representation of 1 is
1 => 00000001 00000000
(on a little endian platform).
So that's why -1
and not 1
: is just a convention; but if you look at the binary representation of the value you may agree that the convention makes sense.
However this convention is far from being universally adopted despite all the above considerations. On many languages true
casted to a numeric value is 1
.
An important note about the code you posted:
You wrote
A=(5=5)
A
is a real variable whose value is represented by 5 bytes (1 for exponent, 4 for mantissa).
The internal C64 representation of the real value 0
is all bits 0
, but the representation of -1
is far from being all bits 1
.
(all bits 1
would lead to the value -1.70141183e+38
)
So again C64 Basic just sticks to a convention.
Finally let's explain the behaviuour of the IF
statements in your code.
Any value different from 0
is evaluated as true
in an IF
statement (this happens on most languages, maybe all).
Bottom note:
If you want to take a look at the internal representation of a C64 variable (integer or real only, not strings) you may use the following code:
READY.
10 REM THE VARIABLE TO INSPECT
20 A=(5=5)
30 REM THE ADDR. OF THE FIRST VARIABLE
40 B=PEEK(45)+256*PEEK(46)
50 REM DISPLAY THE VAR'S 7 BYTES
60 FOR C=B TO B+6
70 PRINT C;": ";PEEK(C)
80 NEXT C
RUN
2227 : 65
2228 : 0
2229 : 129
2230 : 128
2231 : 0
2232 : 0
2233 : 0
Note the first two addresses (2227, 2228) store the variable's name (65, 0 for A
)
You may try declaring the variable as integer and see the result with
20 A%=(5=5)