lue-bird/elm-bits/10.0.1

NOTE: the latest version is 10.1.1

Bits

Convert from and to a List of Bits

fromIntUnsigned : Int -> Int -> List Bit

Convert the unsigned Int to a given number of bits.

import Bit exposing (Bit(..))
import Bits


14 |> Bits.fromIntUnsigned 4
--> [ I, I, I, O ]
fromIntSigned : Int -> Int -> List Bit

Encode a signed Int with 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 Bit exposing (Bit(..))
import Bits


-6 |> Bits.fromIntSigned 5
--> [ I, I, O, I, O ]


10 |> Bits.fromIntSigned 6
--> [ O, O, I, O, I, O ]
fromBytes : Bytes -> List Bit

Convert from Bytes to a list of individual bits.

If you have a need for UTF-8 characters and Floats, feel free to open an issue or PR.

transform

toIntUnsigned : List Bit -> Int

Convert ≤ 32 bits into a natural Int ≥ 0

import Bit exposing (Bit(..))
import Bits


[ I, O, O, I, O, I ]
    |> Bits.toIntUnsigned
--> 37
toIntSigned : List Bit -> Int

Create signed Int from a bit array's in 2's complement encoding using its length 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 Bit exposing (Bit(..))
import Bits


[ O, I, O, I, O ] |> Bits.toIntSigned
--> 10


[ I, O, I, O, I, O, I, O ] |> Bits.toIntSigned
--> -86


[ O, O, O, O, O, I, O, I, O, I, O ]
    |> Bits.toIntSigned
--> 42
toIntUnsigned8s : List Bit -> List Int

Convert to a list of Ints in range 0..255 from a list of individual bits.

import Bit exposing (Bit(..))
import Bits


[ O, O, I, O, O, I, O, I, {- 8 bits -} O, O, I, O, O, I, I, I ]
    |> Bits.toIntUnsigned8s
--> [ 37, 39 ]

From there it's easy to convert to e.g. Bytes using List.map Bytes.Encode.unsignedInt8 |> Bytes.Encode.sequence

You should make sure in your code that, the given bits are a multiple of 8. If there are remaining bits, toIntUnsigned8s treats your input as if it had enough 0s padded to the right.

toIntUnsigned16s : List Bit -> List Int

Convert to a list of Ints in range 0..65535 from a list of individual bits.

import Bit exposing (Bit(..))
import Bits


[ O, O, O, O, O, O, O, O, O, O, I, O, O, I, O, I, {- 16 bits -} O, O, O, O, O, O, O, O, O, O, I, O, O, I, I, I ]
    |> Bits.toIntUnsigned16s
--> [ 37, 39 ]

From there it's easy to convert to e.g. Bytes using List.map Bytes.Encode.unsignedInt16 |> Bytes.Encode.sequence

You should make sure in your code that, the given bits are a multiple of 16. If there are remaining bits, toIntUnsigned16s treats your input as if it had enough 0s padded to the right.

toIntUnsigned32s : List Bit -> List Int

Convert to a list of Ints in range 0..4294967295 from a list of individual bits.

import Bit exposing (Bit(..))
import Bits


[ O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, I, O, O, I, O, I, {- 32 bits -} O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, I, O, O, I, I, I ]
    |> Bits.toIntUnsigned32s
--> [ 37, 39 ]

From there it's easy to convert to e.g. Bytes using List.map Bytes.Encode.unsignedInt32 |> Bytes.Encode.sequence

You should make sure in your code that, the given bits are a multiple of 32. If there are remaining bits, toIntUnsigned32s treats your input as if it had enough 0s padded to the right.

You can also use all the operations that work on List, for example

-- complement
List.map Bit.opposite


-- left shift
(_ ++ List.repeat n Bit.O) |> List.take x


-- left right
(List.repeat n Bit.O ++ _) |> List.take x


...