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
NConvert <= 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
--> 37Int signedEncode 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 ]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
--> 42Pad 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
...
Convert the natural number to a given number of bits (
<= 32).The
Nis always clamped to<= 2 ^ bitCount - 1