(natFold f z n)
> f : (a > Nat > a)
> z : a
> n : Nat
> a
Folds a function over the bits of a natural number.
natFold add 0 5 == 2 ; Sum of bits in 5 (0101)
natFold or 0 10 == 1 ; OR of all bits in 10 (1010)
met
(met n)
> n : Nat
> Nat
Calculates the number of bits required to represent a natural number.
met 0 == 0
met 1 == 1
met 7 == 3
met 8 == 4
met 255 == 8
met 256 == 9
popCount
(popCount n)
> n : Nat
> Nat
Counts the number of set bits in a natural number.
popCount 0 == 0
popCount 1 == 1
popCount 7 == 3 ; 0111 has 3 set bits
popCount 15 == 4 ; 1111 has 4 set bits
trunc
(trunc w n)
> w : Nat
> n : Nat
> Nat
Truncates a natural number to a given bit width.
trunc 3 13 == 5
trunc 2 7 == 3 ; 7 (111) truncated to 2 bits is 3 (11)
trunc 4 15 == 15 ; 15 (1111) truncated to 4 bits is still 15
bitSlice
(bitSlice o w n)
> o : Nat
> w : Nat
> n : Nat
> Nat
Extracts a slice of bits from a natural number.
bitSlice 0 3 13 == 5
bitSlice 1 2 13 == 2 ; Bits 1-2 of 13 (1101) is 2 (10)
bitSlice 2 2 13 == 3 ; Bits 2-3 of 13 (1101) is 3 (11)
setBit
(setBit i n)
> i : Nat
> n : Nat
> Nat
Sets a specific bit in a natural number.
setBit 0 4 == 5
setBit 2 1 == 5 ; Set 3rd bit of 1 (001) to get 5 (101)
setBit 1 6 == 6 ; Setting already-set bit changes nothing
clearBit
(clearBit i n)
> i : Nat
> n : Nat
> Nat
Clears a specific bit in a natural number.
clearBit 0 5 == 4
clearBit 2 7 == 3 ; Clear 3rd bit of 7 (111) to get 3 (011)
clearBit 1 4 == 4 ; Clearing already-clear bit changes nothing
testBit
(testBit i n)
> i : Nat
> n : Nat
> Nat
Tests if a specific bit is set in a natural number.
testBit 0 5 == 1
testBit 1 5 == 0 ; 2nd bit of 5 (101) is not set
testBit 2 5 == 1 ; 3rd bit of 5 (101) is set
Miscellaneous
roundUp
(roundUp x y)
> x : Nat
> y : Nat
> Nat
Rounds a number up to the nearest multiple of another number.
```sire
roundUp 5 3 == 6
roundUp 6 3 == 6
roundUp 7 3 == 9
roundUp 0 3 == 0