ArraySized
of Bit
s
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
fromN : N (N.In (N.Up minX N.To minPlusX) max) -> N range_ -> ArraySized Bit (N.In (N.Up minX N.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 (N.In min_ max_) -> N (N.Min (N.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
signedfromIntSigned : N (N.Exactly (N.On (N.Add1 bitSizeFrom1))) -> Basics.Int -> ArraySized Bit (N.Exactly (N.On (N.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 (N.In min_ max_) -> Basics.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
toChunksOf : N (N.In (N.On (N.Add1 chunkMinFrom1)) (N.Up chunkMaxX N.To (N.Add1 chunkMaxFrom1PlusX))) -> ArraySized Bit (N.In min_ (N.Up maxX N.To maxPlusX)) -> ArraySized (ArraySized Bit (N.In (N.On (N.Add1 chunkMinFrom1)) (N.Up chunkMaxX N.To (N.Add1 chunkMaxFrom1PlusX)))) (N.In (N.Up0 minX_) (N.Up maxX N.To maxPlusX))
Pad the ArraySized
of Bit
s 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 ]
--> ]
O
s at the beginning aren't removed from the original ArraySized