lue-bird/elm-bits/6.0.1

NOTE: the latest version is 10.1.1

Bits

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

fromN : N range_ -> ArraySized Bit (In (Up32 minX_) (Up32 maxX_))

Convert the unsigned integer to <= 32 bits

Combine with atMost to clamp to a lower bit size

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


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

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

import N exposing (n0)


2 ^ 53 - 1
    |> N.atLeastInt n0
    |> Bits.fromN
    |> Bits.toN
    |> N.toInt
--> 2 ^ 32 - 1
toN :
ArraySized Bit (In min_ (Up maxTo32_ To N32))
-> N (Min (Up0 nX_))

Convert <= 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
--> 37

Int

fromIntSigned : N (Exactly bitSize) -> Int -> ArraySized Bit (Exactly bitSize)

Clamp 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 ]
toIntSigned : ArraySized Bit (In min_ (Up maxTo32 To N32)) -> Int

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`
--> 10

padToLength 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.padToLength n8
    |> Bits.toIntSigned
-- 8-bit `Int`
--> -128 + 42

alter

N (In (Fixed paddedMin) (Up maxX To paddedMaxPlusX))
-> ArraySized
(In
(Up paddingMaxPlusX_ To paddedMaxPlusX)
(Up paddingMin_ To paddedMin)
)
-> ArraySized
(In (Fixed paddedMin) (Up maxX To paddedMaxPlusX))

Increase the capacity to a given length by padding with Os to the beginning

Short for

ArraySized.padToLength Down (ArraySized.repeat O)
N (Exactly newLength)
-> ArraySized Bit (In (Fixed min_) (Fixed max_))
-> ArraySized Bit (Exactly newLength)

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.atLeastInt n0
    |> Bits.fromN
    |> Bits.atMost n16
    |> ArraySized.maxTo n16
    |> Bits.toN
    |> N.toInt
--> 2 ^ 16 - 1

Its length after that will be Exactly the given desired length. Some operations like glueing or Bits.toN will stop working because of this. In that case, just

|> Bits.atMost n
|> ArraySized.minTo n
|> ArraySized.maxTo n

To give the ArraySized's type more information

ArraySized Bit (In (Fixed min_) (Fixed max))
-> ArraySized Bit (In (Up0 minX_) (Fixed max))

Remove all O from the beginning until the first I is reached

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


n14 |> Bits.fromN |> Bits.unpad |> ArraySized.toList
--> [ I, I, I, O ]
N (Exactly (Add1 chunkLengthMinus1))
-> ArraySized Bit (In (Fixed min_) (Up maxX To maxPlusX))
-> ArraySized
(ArraySized Bit (Exactly (Add1 chunkLengthMinus1)))
(In (Up0 minX_) (Up maxX To (Add1 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.l1 O
    |> Bits.toChunksOf n8
    |> ArraySized.map ArraySized.toList
    |> ArraySized.toList
--> [ [ O, O, O, O, O, O, O, O ] ]


ArraySized.l3 I I I
    |> ArraySized.glue Up (ArraySized.l8 O I I I O I O O)
    |> ArraySized.glue Up (ArraySized.l8 O I I I O I O O)
    |> ArraySized.glue 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.glue Up (ArraySized.repeat O n...)


-- complement
ArraySized.map Bit.opposite


...