Module

pooka/bit

src/pooka/bit.pooka

Bits 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.

Pooka
import pooka/bit

assert bit.set(0, 3) == Ok(8)

API reference

enum

BitError

src/pooka/bit.pooka:17
enum BitError
variant

NegativeCount

src/pooka/bit.pooka:18
BitError.NegativeCount
variant

NegativePosition

src/pooka/bit.pooka:19
BitError.NegativePosition
function

and

src/pooka/bit.pooka:43
and(a: Int, b: Int) -> Int
INFERREDfn(Int, Int) -> Int

Keep the bit positions set in both integers.

Pooka
import pooka/bit

assert bit.and(6, 3) == 2
function

or

src/pooka/bit.pooka:54
or(a: Int, b: Int) -> Int
INFERREDfn(Int, Int) -> Int

Keep the bit positions set in either integer.

Pooka
import pooka/bit

assert bit.or(4, 2) == 6
function

xor

src/pooka/bit.pooka:65
xor(a: Int, b: Int) -> Int
INFERREDfn(Int, Int) -> Int

Keep the bit positions set in exactly one integer.

Pooka
import pooka/bit

assert bit.xor(6, 3) == 5
function

not

src/pooka/bit.pooka:76
not(value: Int) -> Int
INFERREDfn(Int) -> Int

Complement the integer's bits using Erlang integer bit semantics.

Pooka
import pooka/bit

assert bit.not(0) == -1
function

shift_left

src/pooka/bit.pooka:87
shift_left(value: Int, count: Int) -> Result(Int, BitError)
INFERREDfn(Int, Int) -> Result(Int, pooka/bit.BitError)

Shift left by a nonnegative count, or return NegativeCount.

Pooka
import pooka/bit

assert bit.shift_left(1, 3) == Ok(8)
function

shift_right

src/pooka/bit.pooka:101
shift_right(value: Int, count: Int) -> Result(Int, BitError)
INFERREDfn(Int, Int) -> Result(Int, pooka/bit.BitError)

Arithmetic-shift right by a nonnegative count, preserving the sign.

Pooka
import pooka/bit

assert bit.shift_right(-8, 1) == Ok(-4)
function

is_set

src/pooka/bit.pooka:124
is_set(value: Int, position: Int) -> Result(Bool, BitError)
INFERREDfn(Int, Int) -> Result(Bool, pooka/bit.BitError)

Check a zero-based bit position. Negative positions return NegativePosition.

Pooka
import pooka/bit

assert bit.is_set(8, 3) == Ok(True)
function

set

src/pooka/bit.pooka:136
set(value: Int, position: Int) -> Result(Int, BitError)
INFERREDfn(Int, Int) -> Result(Int, pooka/bit.BitError)

Switch on one zero-based bit, returning a new integer.

Pooka
import pooka/bit

assert bit.set(0, 3) == Ok(8)
function

clear

src/pooka/bit.pooka:148
clear(value: Int, position: Int) -> Result(Int, BitError)
INFERREDfn(Int, Int) -> Result(Int, pooka/bit.BitError)

Switch off one zero-based bit, returning a new integer.

Pooka
import pooka/bit

assert bit.clear(10, 1) == Ok(8)