bitfield ( bitspec -- quot )
Factor handbook > The language > Numbers > Arithmetic > Bitwise arithmetic > Additional bitwise arithmetic > Constructing bit fields


Vocabulary
math.bitwise

Inputs and outputs
values...a series of objects
bitspecan array
nan integer


Word description
Constructs an integer 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:
{ constant shift } - the resulting bit field is bitwise or'd with constant shifted to the right by shift bits
{ word shift } - the resulting bit field is bitwise or'd with word applied to the top of the stack; the result is shifted to the right by shift bits
shift - the resulting bit field is bitwise or'd with the top of the stack; the result is shifted to the right by shift bits

The bit field specifier is processed left to right, so stack values should be supplied in reverse order.

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:
: baz-bitfield ( x y z -- n ) { { fooify 16 } { 1 15 } 11 0 } bitfield ;


Definition