Module
pooka/bit
src/pooka/bit.pookaBits are tiny lamps; each one has a numbered switch.
Use arbitrary-width integer bitwise operations. Checked shifts and bit positions reject negative counts. Right shift is arithmetic, so negative integers keep their sign.
import pooka/bit
assert bit.set(0, 3) == Ok(8)
API reference
enum BitError
BitError.NegativeCount
BitError.NegativePosition
and(a: Int, b: Int) -> Int
INFERRED
fn(Int, Int) -> IntKeep the bit positions set in both integers.
import pooka/bit
assert bit.and(6, 3) == 2
or(a: Int, b: Int) -> Int
INFERRED
fn(Int, Int) -> IntKeep the bit positions set in either integer.
import pooka/bit
assert bit.or(4, 2) == 6
xor(a: Int, b: Int) -> Int
INFERRED
fn(Int, Int) -> IntKeep the bit positions set in exactly one integer.
import pooka/bit
assert bit.xor(6, 3) == 5
not(value: Int) -> Int
INFERRED
fn(Int) -> IntComplement the integer's bits using Erlang integer bit semantics.
import pooka/bit
assert bit.not(0) == -1
shift_left(value: Int, count: Int) -> Result(Int, BitError)
INFERRED
fn(Int, Int) -> Result(Int, pooka/bit.BitError)Shift left by a nonnegative count, or return NegativeCount.
import pooka/bit
assert bit.shift_left(1, 3) == Ok(8)
shift_right(value: Int, count: Int) -> Result(Int, BitError)
INFERRED
fn(Int, Int) -> Result(Int, pooka/bit.BitError)Arithmetic-shift right by a nonnegative count, preserving the sign.
import pooka/bit
assert bit.shift_right(-8, 1) == Ok(-4)
is_set(value: Int, position: Int) -> Result(Bool, BitError)
INFERRED
fn(Int, Int) -> Result(Bool, pooka/bit.BitError)Check a zero-based bit position. Negative positions return NegativePosition.
import pooka/bit
assert bit.is_set(8, 3) == Ok(True)
set(value: Int, position: Int) -> Result(Int, BitError)
INFERRED
fn(Int, Int) -> Result(Int, pooka/bit.BitError)Switch on one zero-based bit, returning a new integer.
import pooka/bit
assert bit.set(0, 3) == Ok(8)
clear(value: Int, position: Int) -> Result(Int, BitError)
INFERRED
fn(Int, Int) -> Result(Int, pooka/bit.BitError)Switch off one zero-based bit, returning a new integer.
import pooka/bit
assert bit.clear(10, 1) == Ok(8)