keen/bits

Source

Functions for operating on the individual bits in nats / ints.

Bit indexes are counted from the right, so the least-siginificant digit is 0. So, the number 5 has bits 0 and 2 set, since 5 is 2**0 + 2**2.

wrap-to int8(a nat8)
wrap-to int8(a nat16)
wrap-to int8(a nat32)
wrap-to int8(a nat64)
wrap-to int8(a int16)
wrap-to int8(a int32)
wrap-to int8(a int64)
wrap-to int16(a int32)
wrap-to int16(a int64)
wrap-to int16(a nat16)
wrap-to int16(a nat32)
wrap-to int16(a nat64)
wrap-to int32(a int64)
wrap-to int32(a nat32)
wrap-to int32(a nat64)
wrap-to int64(a nat64)
wrap-to nat8(a int8)
wrap-to nat8(a int16)
wrap-to nat8(a int32)
wrap-to nat8(a int64)
wrap-to nat8(a nat16)
wrap-to nat8(a nat32)
wrap-to nat8(a nat64)
wrap-to nat16(a int16)
wrap-to nat16(a int32)
wrap-to nat16(a int64)
wrap-to nat16(a nat32)
wrap-to nat16(a nat64)
wrap-to nat32(a int32)
wrap-to nat32(a int64)
wrap-to nat32(a nat64)
wrap-to nat64(a int64)
wrap-add nat8(a nat8, b nat8)
wrap-add nat16(a nat16, b nat16)
wrap-add nat32(a nat32, b nat32)
wrap-add nat64(a nat64, b nat64)
wrap-add int8(a int8, b int8)
wrap-add int16(a int16, b int16)
wrap-add int32(a int32, b int32)
wrap-add int64(a int64, b int64)
wrap-sub nat8(a nat8, b nat8)
wrap-sub nat16(a nat16, b nat16)
wrap-sub nat32(a nat32, b nat32)
wrap-sub nat64(a nat64, b nat64)
wrap-sub int8(a int8, b int8)
wrap-sub int16(a int16, b int16)
wrap-sub int32(a int32, b int32)
wrap-sub int64(a int64, b int64)
wrap-mul nat8(a nat8, b nat8)
wrap-mul nat16(a nat16, b nat16)
wrap-mul nat32(a nat32, b nat32)
wrap-mul nat64(a nat64, b nat64)
wrap-mul int8(a int8, b int8)
wrap-mul int16(a int16, b int16)
wrap-mul int32(a int32, b int32)
wrap-mul int64(a int64, b int64)
~ nat8(a nat8)
Negates every bit. 0 bits become 1 bits and vice versa.
~ nat16(a nat16)
~ nat32(a nat32)
~ nat64(a nat64)
& nat8(a nat8, b nat8)
Intersection of bits; 'and's each corresponding bit in parallel. An output bit will be 1 iff both corresponding input bits are 1.
& nat16(a nat16, b nat16)
& nat32(a nat32, b nat32)
& nat64(a nat64, b nat64)
| nat8(a nat8, b nat8)
Union of bits; 'or's each corresponding bit in parallel. An output bit will be 1 iff either corresponding input bit is 1.
| nat16(a nat16, b nat16)
| nat32(a nat32, b nat32)
| nat64(a nat64, b nat64)
^ nat8(a nat8, b nat8)
Performs exclusive or (xor) of each bit in parallel. Output bits will be 1 iff the corresponding input bits are not equal.
^ nat16(a nat16, b nat16)
^ nat32(a nat32, b nat32)
^ nat64(a nat64, b nat64)
<< nat8(a nat8, b nat8)
Shifts the bits of 'a' left by 'b' bits. Does not rotate.
<< nat16(a nat16, b nat16)
<< nat32(a nat32, b nat32)
<< nat64(a nat64, b nat64)
>> nat8(a nat8, b nat8)
Shifts the bits of a right by b bits. Does not rotate.
>> nat16(a nat16, b nat16)
>> nat32(a nat32, b nat32)
>> nat64(a nat64, b nat64)
rotate-left nat8(a nat8, amount nat8)
rotate-left nat16(a nat16, amount nat16)
rotate-left nat32(a nat32, amount nat32)
rotate-left nat64(a nat64, amount nat64)
rotate-right nat8(a nat8, amount nat8)
rotate-right nat16(a nat16, amount nat16)
rotate-right nat32(a nat32, amount nat32)
rotate-right nat64(a nat64, amount nat64)
bits-intersect bool(a nat8, b nat8)
True iff the numbers have at least one bit in common.
bits-intersect bool(a nat16, b nat16)
bits-intersect bool(a nat32, b nat32)
bits-intersect bool(a nat64, b nat64)
has-all-bits bool(a nat8, b nat8)
True if for every 1 bit in b, the corresponding bit in a is also a 1.
has-all-bits bool(a nat16, b nat16)
has-all-bits bool(a nat32, b nat32)
has-all-bits bool(a nat64, b nat64)
nth-bit nat32(bit-index nat32)
Number with only the bit at bit-index set. Same as 1 << bit-index.
nth-bit nat64(bit-index nat64)
has-bit bool(a nat32, bit-index nat32)
True if the bit at bit-index is set.
has-bit bool(a nat64, bit-index nat64)
add-bit nat32(a nat32, bit-index nat32)
Sets the bit at bit-index to 1.
add-bit nat64(a nat64, bit-index nat64)
remove-bit nat32(a nat32, bit-index nat32)
Sets the bit at bit-index to 0.
remove-bit nat64(a nat64, bit-index nat64)
count-ones nat32(a nat32)
Returns the number of 1 bits in a.
count-ones nat32(a nat64)
one-bits one-bits32(bits nat32)
one-bits one-bits64(bits nat64)
one-bits32 record
bits nat32
size nat32(a one-bits32)
some bool(a one-bits32, f bool mut(nat32))
Calls f on the index of each 1 bit.
one-bits64 record
bits nat64
size nat32(a one-bits64)
some bool(a one-bits64, f bool mut(nat64))
Calls f on the index of each 1 bit.
nth-one-bit nat32 option(word nat32, n nat32)
Returns the index of the nth 1 bit in the word. (Low bits have low indices, so the 1s place is index 0, the 2s place is index 1, etc.) Returns an empty option unless there are at least n + 1 one bits in word.
nth-one-bit nat64 option(word nat64, n nat64)
bits-cat nat16(a nat8, b nat8)
bits-cat nat32(a nat16, b nat16)
bits-cat nat64(a nat32, b nat32)
Concatenates two 32-bit numbers to a 64-bit number. a will be the high bits and b will be the low bits.
high-n-bits nat8(n nat8)
Number with the highest n bits set.
high-n-bits nat16(n nat16)
high-n-bits nat32(n nat32)
high-n-bits nat64(n nat64)
low-n-bits nat8(n nat8)
Number with the lowest n bits set.
low-n-bits nat16(n nat16)
low-n-bits nat32(n nat32)
low-n-bits nat64(n nat64)
log-2-rounded-up nat64(a nat64)
log-2-rounded-down nat64(a nat64)
trailing-zeroes nat32(a nat32)
Number of 0s in the binary representation of a number
trailing-zeroes nat64(a nat64)
is-power-of-two bool(a nat64)
round-up-to-power-of-two nat32(a nat32)
round-up-to-power-of-two nat64(a nat64)