Search code examples
basicc64

Why does PRINT'ing a true boolean expression output -1?


In Commodore 64 BASIC V2, PRINT'ing a true boolean expression outputs -1:

READY.
A=(5=5)

READY.
PRINT A
-1

Why -1 and not 1?

enter image description here


Solution

  • Commodore Basic doesn't have a boolean data type. A boolean expression evaluates to a number, where 0 means False and -1 means true.

    As there is no boolean data type there are no boolean type expressions. You can use any numeric expression in an IF statement, and it will interpret any non-zero value as True.

    Some languages where a boolean value is numeric or where it can be converted to a numeric value uses -1 to represent a true value. For an integer value 0 all bits are cleared, and for -1 all bits are set, so they can be seen as natural complements for each other.

    Eventhough Commodore Basic doesn't use integer numbers but floating point numbers, the value -1 was supposedly chosen because some other languages uses it.