Dextrin.Bits (Dextrin v0.3.2)

Copy Markdown View Source

bits (DXN.md §1.4): a run of bits that is not whole bytes, written as its bytes and their bit count -- @bits {"oA==" 3} in text, T_BITS around [bytes, count] in .dxnb.

Bits and bytes are one value space. An Elixir bitstring is the value, held in a Dextrin.Bytes like any bytes; one whose size is a multiple of 8 is a binary, and is written as bytes. This module is the one place the two halves of the written form are made and checked, so text, binary and the canonical form cannot disagree about them.

Summary

Functions

The bits data and count write, or :error when they do not write any: count must be a non-negative integer and data exactly the bytes it needs. The unused low bits of the last byte are ignored.

bits as written: its bytes, the unused low bits of the last one zero, and how many of their bits are the value.

Functions

from_parts(data, count)

@spec from_parts(term(), term()) :: {:ok, bitstring()} | :error

The bits data and count write, or :error when they do not write any: count must be a non-negative integer and data exactly the bytes it needs. The unused low bits of the last byte are ignored.

iex> Dextrin.Bits.from_parts(<<0b10100111>>, 3)
{:ok, <<5::3>>}

iex> Dextrin.Bits.from_parts(<<1, 2>>, 3)
:error

parts(bits)

@spec parts(bitstring()) :: {binary(), non_neg_integer()}

bits as written: its bytes, the unused low bits of the last one zero, and how many of their bits are the value.

iex> Dextrin.Bits.parts(<<5::3>>)
{<<0b10100000>>, 3}