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 an unsigned integer: N (Min (Up0 nX_))
import N
import Bit exposing (Bit(..))
import ArraySized
ArraySized.l6 I O O I O I
|> Bits.toN
|> N.toInt
--> 37IntClamp a signed Int, decoding a given bit size n
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 |> Bits.fromIntSigned n5 |> ArraySized.toList
--> [ O, I, O, I, O ]
10 |> Bits.fromIntSigned n6 |> ArraySized.toList
--> [ I, O, I, O, I, O ]Create signed Int
encoded in its length as the Int's bit size
Encoding as follows:
given bit size n,
encode 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 |> Bits.toIntSigned
-- 5-bit `Int`
--> -6
ArraySized.l6 I O I O I O |> Bits.toIntSigned
-- 6-bit `Int`
--> 10padToAtLeast to convert it to a higher-bit representation
import ArraySized
import Bit exposing (Bit(..))
import N exposing (n8)
ArraySized.l6 I O I O I O
|> Bits.padToAtLeast n8
|> Bits.toIntSigned
-- 8-bit `Int`
--> -128 + 42Increase the capacity to a given length
by padding with Os to the beginning
Short for
ArraySized.padToAtLeast Down lengthMinimum (ArraySized.repeat O)If the ArraySized has a I earlier than a given number of Bits up,
returns only Is up to the given length
import N exposing (n0, n16)
import ArraySized
2 ^ 25
|> N.intToAtLeast n0
|> Bits.fromN
|> Bits.takeAtMost n16
|> ArraySized.maxTo n16
|> Bits.toN
|> N.toInt
--> 2 ^ 16 - 1Its length after that will be Exactly the given desired length.
Some operations like attaching or Bits.toN will stop working because of this.
In that case, just
|> Bits.takeAtMost n
|> ArraySized.minTo n
|> ArraySized.maxTo nTo give the ArraySized's type more information
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
|> Bits.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)
|> Bits.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...)
-- complement
ArraySized.map Bit.opposite
...
Convert the unsigned integer to 32 bits
Combine with
takeAtMostto clamp to a lower bit sizeThe
Nis always clamped to<= 2 ^ 32 - 1