Vocabulary
math.bitwise

Inputs
bitspecan array


Outputs
None

Word description
Constructs an integer (bit field) from a series of values on the stack together with a bit field specifier, which is an array whose elements have one of the following shapes:
{ word shift } - word is applied to the top of the stack and the result is shifted to the left by shift bits and bitor'd with the bit field
shift - the top of the stack is shifted to the left by shift bits and bitor'd with the bit field
{ constant shift } - constant is shifted to the left by shift bits and bitor'd with the bit field

The last entry in the bit field specifier is processed in reverse, so stack values are supplied in reverse order, e.g. the leftmost stack value is the last bit field specifier.

Examples
Consider the following specification:
bits 0-10 are set to the value of x
bits 11-14 are set to the value of y
bit 15 is always on
bits 16-20 are set to the value of fooify applied to z

Such a bit field construction can be specified with a word like the following:
USING: math math.bitwise prettyprint ; IN: math.bitwise.examples : fooify ( x -- y ) 0b1111 bitand ; : baz-bitfield ( x y z -- n ) { { fooify 16 } { 1 15 } 11 0 } bitfield ; 3 2 1 baz-bitfield .
102403

Square the 3 from the stack and shift 8, place the 1 from the stack at bit 5, and shift a constant 1 to bit 2:
USING: math math.bitwise prettyprint ; 1 3 { { sq 8 } 5 { 1 2 } } bitfield .b
0b100100100100


See also
bitfield*

Definition