lue-bird/elm-bits/9.0.2

NOTE: the latest version is 10.1.1

BitArray

ArraySized of Bits

Use what's available in ArraySized to create and analyze bits!

Use the helpers here in Bits when you know the exact number of bits, like in an id

N

N (In (Up minX To minPlusX) max)
-> N range_
-> ArraySized Bit (In (Up minX To minPlusX) max)

Convert the natural number to a given number of bits (<= 32).

import N exposing (n4, n14)
import Bit exposing (Bit(..))
import ArraySized


n14 |> BitArray.fromN n4 |> ArraySized.toList
--> [ I, I, I, O ]

The N is always clamped to <= 2 ^ bitCount - 1

import N exposing (n0)
import N.Local exposing (n32)


2 ^ 53 - 1
    |> N.intToAtLeast n0
    |> BitArray.fromN n32
    |> BitArray.toN
    |> N.toInt
--> 2 ^ 32 - 1
toN : ArraySized Bit (In min_ max_) -> N (Min (Up0 nX_))

Convert <= 32 bits into a natural number: N (Min (Up0 _))

import N
import Bit exposing (Bit(..))
import ArraySized


ArraySized.l6 I O O I O I
    |> BitArray.toN
    |> N.toInt
--> 37

Int signed

N (Exactly (On (Add1 bitSizeFrom1)))
-> Int
-> ArraySized Bit (Exactly (On (Add1 bitSizeFrom1)))

Encode with a given bit size n (<= 32) ranging from -(2 ^ (n - 1)) to 2 ^ (n - 1) - 1. For example, bit size 5 ranges from -16 to 15

import ArraySized
import Bit exposing (Bit(..))
import N exposing (n5, n6)


-6 |> BitArray.fromIntSigned n5 |> ArraySized.toList
--> [ I, I, O, I, O ]


10 |> BitArray.fromIntSigned n6 |> ArraySized.toList
--> [ O, O, I, O, I, O ]
toIntSigned : ArraySized Bit (In min_ max_) -> Int

Create signed Int from a bit array's in 2's complement encoding using its length (<= 32) as the Int's bit count.

So given bit count n, decodes in the range -(2 ^ (n - 1)) → 2 ^ (n - 1) - 1. For example, bit size 5 ranges from -16 to 15

import ArraySized
import Bit exposing (Bit(..))
import N exposing (n5, n6)


ArraySized.l5 O I O I O |> BitArray.toIntSigned
--> 10


ArraySized.l8 I O I O I O I O |> BitArray.toIntSigned
--> -86


ArraySized.l11 O O O O O I O I O I O |> BitArray.toIntSigned
--> 42

alter

N
(In
(On (Add1 chunkMinFrom1))
(Up chunkMaxX To (Add1 chunkMaxFrom1PlusX))
)
-> ArraySized Bit (In min_ (Up maxX To maxPlusX))
-> ArraySized
(ArraySized
(In
(On (Add1 chunkMinFrom1))
(Up chunkMaxX To (Add1 chunkMaxFrom1PlusX))
)
)
(In (Up0 minX_) (Up maxX To maxPlusX))

Pad the ArraySized of Bits to chunks of a given length

import ArraySized
import Linear exposing (Direction(..))
import N exposing (n8)
import Bit exposing (Bit(..))


ArraySized.one O
    |> BitArray.toChunksOf n8
    |> ArraySized.map ArraySized.toList
    |> ArraySized.toList
--> [ [ O, O, O, O, O, O, O, O ] ]


ArraySized.l3 I I I
    |> ArraySized.attach Up (ArraySized.l8 O I I I O I O O)
    |> ArraySized.attach Up (ArraySized.l8 O I I I O I O O)
    |> ArraySized.attach Up (ArraySized.l8 O I I I O I O O)
    |> BitArray.toChunksOf n8
    |> ArraySized.map ArraySized.toList
    |> ArraySized.toList
--> [ [ O, O, O, O, O, I, I, I ]
--> , [ O, I, I, I, O, I, O, O ]
--> , [ O, I, I, I, O, I, O, O ]
--> , [ O, I, I, I, O, I, O, O ]
--> ]

Os at the beginning aren't removed from the original ArraySized

You can additionally use the operations of ArraySized, for example

-- left shift
ArraySized.attach Up (ArraySized.repeat O n)


-- left right
ArraySized.attach Down (ArraySized.repeat O n)


-- complement
ArraySized.map Bit.opposite


...