13
votes

I know the AND word defines binary and... but what defines logical and?

2

2 Answers

12
votes

The same word, AND, is also used for logical and. But the two input values to AND are recommended to be well-formed flags; true and false are represented by two values, bits all set (-1) and bits all unset (0). Other values than these may work as true (as in C), but it may lead to subtle errors.

All comparison operators return well-formed flags, but for instance - does not. The following evaluates to false (0).

7 5 - 7 3 - AND

AND gets bit patterns 100 and 010. The result is 0 (as it does the bitwise and).

References:

  1. Bit Manipulations in Forth, part of Learning Forth Bit by Bit.
  2. Section "A Little Logic" in Chapter 4 of Starting Forth.
0
votes

The Forth AND is a bit-wise AND on 64 bit values. Of course these operators work okay with masks. But if these values are all ones or all zeroes, the result is also all ones or all zeroes (the same is true for bit-wise OR and XOR INVERT operations.).

A boolean flag in Forth is all ones or all zeroes. So if the inputs are boolean flags, the output of AND OR XOR INVERT are also booleans and those operators can thus be used to represent boolean operators. Please note that operations like = < 0= result in a boolean flag.

The situation is the same with +. Because integers are defined as two-complement, + (plus) can be used for signed as well as unsigned numbers. So there is no separate name for unsigned addition of integers.