Skip to content
heff

bits

Bits, the prelude’s: a bitstring of any length, not changed once made — a type of its own beside Bytes, which are whole bytes. A C program keeps them as bytes and how many bits of them there are, a JavaScript one as a Uint8Array whose bits is their length. Import it by a name, import bits as Bits, as its names are the prelude’s lists’ too: Bits.length, Bits.toBytes. A binary writes them, b[5::3], and takes them apart; their equality, instanceEqualityBits, is the prelude’s, as == is, and so are their Debug, instanceDebugBits, which writes them as a binary does, and their Show, instanceShowBits, which writes each bit, 0 or 1.

length

length : Bits -> Int

How many bits.

>>> length b[5::3, "a"]
11

index

index : Bits -> Int -> Maybe Bool

The bit at i, if there is one (True is 1): out of range, Nothing.

>>> List.map (index b[5::3]) [0, 1, 2, 3]
[Just True, Just False, Just True, Nothing]

toList

toList : Bits -> List Bool

Each bit, True where it is 1.

>>> toList b[6::3]
[True, True, False]

toBytes

toBytes : Bits -> Maybe Bytes

Bits that are whole bytes, as Bytes: of a length that is no multiple of 8, Nothing.

>>> toBytes b[1::4, 2::4, b[]::bits]
Just b[18]
>>> toBytes b[1::4]
Nothing

empty

empty : Bits

None.

>>> length empty
0

fromBytes

fromBytes : Bytes -> Bits

Bytes as bits, 8 to a byte.

>>> fromBytes b["a"] == b[97::8, b[]::bits]
True

fromList

fromList : List Bool -> Bits

A bit each, 1 where it is True.

>>> fromList [True, False, True]
b[5::3]

fromInt

fromInt : Int -> Int -> Bits

The integer v’s low n bits, 0 to 64, the most significant first, as v::n writes them: of another n, abandoned.

>>> fromInt 3 13
b[5::3]

slice

slice : Bits -> Int -> Int -> Bits

The bits from i up to j, j not among them, each clamped to them.

>>> List.map show [slice (fromBytes b[0x5a]) 1 4, slice (fromBytes b[0x5a]) 6 20, slice (fromBytes b[0x5a]) 5 2]
["101", "10", ""]

append

append : Bits -> Bits -> Bits

The bits of a, then those of b.

>>> append b[1::1] b[2::2]
b[6::3]

concat

concat : List Bits -> Bits

Each one’s bits, one after another, made at once.

>>> concat [b[1::1], empty, b[1::2], fromBytes b[255]]
b[191, 7::3]

eq

eq : Bits -> Bits -> Bool

Whether they are the same bits, as == says (the prelude’s instanceEqualityBits).

>>> eq b[5::3] (fromList [True, False, True])
True