Convert from and to a List of Bits
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 ]Convert from a list of individual bits to Bytes.
If the size of the input list is not a multiple of 8 this function will pad it to the right with 0s.
If you have a need for UTF-8 characters and Floats, feel free to open an issue or PR.
Convert ≤ 32 bits into a natural Int ≥ 0
import Bit exposing (Bit(..))
import Bits
[ I, O, O, I, O, I ]
|> Bits.toIntUnsigned
--> 37Create 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
--> 42Convert 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.
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.
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
...
Convert the unsigned
Intto a given number of bits.