Additional bitwise arithmetic - Factor Documentation (original) (raw)
The math.bitwise vocabulary provides bitwise arithmetic words extending Bitwise arithmetic. They are useful for efficiency, low-level programming, and interfacing with C libraries.
Extracting bits from an integer:
unmask ( x n -- y )
mask? ( x n -- ? )
unmask? ( x n -- ? )
Generating an integer with n set bits:
Counting the number of set bits:
Testing the parity of an object:
More efficient modding by powers of two:
bitroll-32 ( m s -- n )
bitroll-64 ( m s -- n )
w- ( x y -- z )
w* ( x y -- z )
W- ( x y -- z )
W* ( x y -- z )
Words for taking larger integers apart into smaller integers:
w>h/h ( w -- h1 h2 )
h>b/b ( h -- b1 b2 )
Converting a number to the nearest even/odd/signed:
>odd ( m -- n )
>signed ( x n -- y )
Bitfields: Constructing bit fields