Node:Flags and Comparisons Tutorial, Next:, Previous:Conditional execution Tutorial, Up:Tutorial



Flags and Comparisons

In a false-flag all bits are clear (0 when interpreted as integer). In a canonical true-flag all bits are set (-1 as a twos-complement signed integer); in many contexts (e.g., if) any non-zero value is treated as true flag.

false .
true .
true hex u. decimal

Comparison words produce canonical flags:

1 1 = .
1 0= .
0 1 < .
0 0 < .
-1 1 u< . \ type error, u< interprets -1 as large unsigned number
-1 1 < .

Gforth supports all combinations of the prefixes 0 u d d0 du f f0 (or none) and the comparisons = <> < > <= >=. Only a part of these combinations are standard (for details see the standard, Numeric comparison, Floating Point or Word Index).

You can use and or xor invert can be used as operations on canonical flags. Actually they are bitwise operations:

1 2 and .
1 2 or .
1 3 xor .
1 invert .

You can convert a zero/non-zero flag into a canonical flag with 0<> (and complement it on the way with 0=).

1 0= .
1 0<> .

You can use the all-bits-set feature of canonical flags and the bitwise operation of the Boolean operations to avoid ifs:

: foo ( n1 -- n2 )
  0= if
    14
  else
    0
  endif ;
0 foo .
1 foo .

: foo ( n1 -- n2 )
  0= 14 and ;
0 foo .
1 foo .
Assignment:
Write min without if.

For reference, see Boolean Flags, Numeric comparison, and Bitwise operations.