tags:

views:

61

answers:

1

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

+1  A: 

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 may lead to subtle errors.

All comparison operators return well-formed flags, but for instance - does not. This returns false (0):

7 5 - 7 3 - AND

AND gets bit patterns 100 and 010. The result is 0 (as it does the binary 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.
Peter Mortensen
Don't know enough forth to say if this is valid, but the bit patterns 0xFF00 and 0x00FF have bitwise and 0x0000 but logical and True (since both are nonzero)
TokenMacGuy
@TokenMacGuy: Thanks! It was such an example I was looking for. I have updated the answer.
Peter Mortensen