The Forth code
7 3 > . (7 > 3)
returns -1, but every other language I've ever used uses 1 as a flag for true. Why is this? What accounts for this difference?
-1 is all bits set which then has the benefit that words such as and, or, not, ... serve as both logical and bitwise operators (as opposed to say C with &&, ||, !, ... vs. &, |, ~, ...)
Per the 1994 standard:
Flags Flags may have one of two logical states, true or false. Programs that use flags as arithmetic operands have an environmental dependency. A true flag returned by a standard word shall be a single-cell value with all bits set. A false flag returned by a standard word shall be a single-cell value with all bits clear.
So true is not -1, it's all bits set — the logical opposite of no bits set. In your particular environment, all bits set is -1, presumably because your computer uses two's complement arithmetic. But it doesn't have to in order to run Forth and therefore true doesn't have to be -1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With