AND OR XOR LSHIFT RSHIFT INVERT
AND ( n1 n2 --- n3)
Execute logic AND.
Example:
false false and . \ display 0 false true and . \ display 0 true false and . \ display 0 true true and . \ display -1
OR ( n1 n2 --- n3)
Execute logic OR.
Example:
false false or . \ display 0 false true or . \ display -1 true false or . \ display -1 true true or . \ display -1
XOR ( x1 x2 -- x3 )
Execute logic eXclusif OR.
Example:
false false xor . \ display 0 false true xor . \ display -1 true false xor . \ display -1 true true xor . \ display 0
ATTENTION
The words AND
, OR
, and XOR
perform operations
binary bitwise logic on single-precision integers at the top of
the data stack.
If you take other values than FALSE
or TRUE
, the result
risk of not being the expected result:
hex 40 20 and . \ display 0
However, this bitwise logical manipulation is interesting for managing ports on a microcontroller for example by managing mask bits.
Example, to separate a byte in distinct values:
hex 3c 0f and . \ display c mask is 0F (00001111)
To recover the upper part of the byte:
hex 3c f0 and . \ display 3 mask is F0 (11110000)
LSHIFT ( x1 u -- x2 )
Shift to the left of u bits by the value x1.
Example:
8 2 lshift . \ display 32
RSHIFT ( x1 u -- x2 )
Right shift of the value x1 by u bits.
Example:
64 2 rshift . \ display 16
INVERT ( x1 -- x2 )
Complement to one of x1. Acts on 16 or 32 bits depending on the FORTH versions.
Example:
1 invert . \ display -2 (%1111111111111110)
DINVERT ( d1 -- d2 )
Bit inversion on 32 bits.