Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Forth return -1 as a flag for True?

Tags:

forth

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?

like image 872
Spasiu Avatar asked May 23 '14 15:05

Spasiu


2 Answers

-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. &, |, ~, ...)

like image 100
AshleyF Avatar answered Oct 30 '22 08:10

AshleyF


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.

like image 45
Tommy Avatar answered Oct 30 '22 08:10

Tommy