View Source Abit (abit v0.3.3)
Abit let's you use :atomics
as a bit array or as an array of N-bit counters.
To know more about :atomics
check the Erlang atomics documentation
The Abit
module (this module) has functions to use :atomics
as a bit array.
The bit array is zero indexed.
The Abit.Counter
module has functions to use :atomics
as an array of N-bit
counters.
The functions in Abit.Bitmask
help working with bitmasks.
Abit
iex> ref = :atomics.new(100, signed: false)
iex> Abit.bit_count(ref)
6400
iex> Abit.bit_at(ref, 0)
0
iex> Abit.set_bit_at(ref, 0, 1)
:ok
iex> Abit.bit_at(ref, 0)
1
Abit.Counter
iex> counter = %Abit.Counter{} = Abit.Counter.new(100, 16)
iex> Abit.Counter.get(counter, 0)
0
iex> Abit.Counter.put(counter, 0, 100)
{:ok, {0, 100}}
iex> Abit.Counter.add(counter, 0, 100)
{:ok, {0, 200}}
iex> Abit.Counter.member?(counter, 200)
true
Abit.Bitmask
iex> Abit.Bitmask.set_bits_count(3)
2
iex> Abit.Bitmask.bit_at(2, 0)
0
iex> Abit.Bitmask.bit_at(2, 1)
1
Summary
Functions
Returns bit at bit_index
in atomic ref
.
Returns total count of bits in atomics ref
.
Returns position of bit in :atomics
.
Returns the bitwise Hamming distance between the two
given :atomics
references ref_l
and ref_r
.
Bit intersection of atomics using Bitwise AND operator.
Merge bits of atomics ref_a
& ref_b
using the
bitwise OR operator.
Sets the bit at bit_index
to bit
in the atomics ref
.
Returns number of bits set to 1 in atomics array ref
.
Returns a flat list of every atomic value converted
into a list of bits from ref
atomics reference.
Functions
@spec bit_at(reference(), non_neg_integer()) :: 0 | 1
Returns bit at bit_index
in atomic ref
.
Examples
iex> ref = :atomics.new(1, signed: false)
iex> ref |> :atomics.put(1, 3)
iex> Abit.bit_at(ref, 0)
1
iex> Abit.bit_at(ref, 1)
1
iex> Abit.bit_at(ref, 2)
0
@spec bit_count(reference()) :: pos_integer()
Returns total count of bits in atomics ref
.
:atomics
are 64 bit integers so total
count of bits is size * 64.
Examples
iex> ref = :atomics.new(1, signed: false)
iex> Abit.bit_count(ref)
64
iex> ref2 = :atomics.new(2, signed: false)
iex> Abit.bit_count(ref2)
128
@spec bit_position(non_neg_integer()) :: {pos_integer(), non_neg_integer()}
Returns position of bit in :atomics
.
Returns a 2 tuple containing:
atomics_index
- the index of the integer in atomics where the bit is locatedbit_index
- the index of the bit in the integer
Examples
iex> Abit.bit_position(0)
{1, 0}
iex> Abit.bit_position(11)
{1, 11}
iex> Abit.bit_position(64)
{2, 0}
@spec hamming_distance(reference(), reference()) :: non_neg_integer()
Returns the bitwise Hamming distance between the two
given :atomics
references ref_l
and ref_r
.
Raises ArgumentError if the sizes of ref_l
and ref_r
are not equal.
Examples
iex> ref_l = :atomics.new(10, signed: false)
iex> ref_r = :atomics.new(10, signed: false)
iex> Abit.hamming_distance(ref_l, ref_r)
0
iex> ref_l |> :atomics.put(1, 7)
iex> Abit.hamming_distance(ref_l, ref_r)
3
Bit intersection of atomics using Bitwise AND operator.
Returns ref_a
mutated.
Merge bits of atomics ref_a
& ref_b
using the
bitwise OR operator.
ref_b
will be merged into ref_a
.
Returns ref_a
mutated.
@spec set_bit_at(reference(), non_neg_integer(), 0 | 1) :: :ok
Sets the bit at bit_index
to bit
in the atomics ref
.
Returns :ok
.
Examples
iex> ref = :atomics.new(1, signed: false)
iex> ref |> Abit.set_bit_at(0, 1)
iex> ref |> :atomics.get(1)
1
iex> ref |> Abit.set_bit_at(0, 0)
:ok
iex> ref |> :atomics.get(1)
0
@spec set_bits_count(reference()) :: non_neg_integer()
Returns number of bits set to 1 in atomics array ref
.
Examples
iex> ref = :atomics.new(1, signed: false)
iex> ref |> :atomics.put(1, 3)
iex> Abit.set_bits_count(ref)
2
iex> ref2 = :atomics.new(1, signed: false)
iex> Abit.set_bits_count(ref2)
0
@spec to_list(reference()) :: [0 | 1]
Returns a flat list of every atomic value converted
into a list of bits from ref
atomics reference.
Examples
ref = :atomics.new(10, signed: false)
ref |> Abit.to_list
[0, 0, 0, 0, 0, ...]