문제

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

도움이 되었습니까?

해결책

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. 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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top