abit v0.1.4 Abit View Source
Use :atomics as a bit array or as an array of counters with n bits per counter in Elixir.
The Abit module (this module) has functions to use an :atomics as a bit array. The Abit.Counter module has functions to create an array of counters and manipulate them.
Link to this section Summary
Functions
Returns bit at bit_index
in atomic ref
.
Returns number of bits in atomics ref
.
Returns a 2 tuple containing
Returns the bitwise hamming distance of two :atomics
references.
Bit intersection of atomics using Bitwise AND operator.
Bit merge atomics using Bitwise OR operator.
ref_b
will be merged into ref_a
.
Sets the bit at bit_index
to bit
in the atomic ref
.
Returns number of bits set to 1 in atomics array ref
.
Link to this section Functions
bit_at(ref, bit_index)
View Sourcebit_at(reference(), non_neg_integer()) :: 0 | 1
Returns bit at bit_index
in atomic ref
.
Example
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
Returns number of bits in atomics ref
.
Atomics are 64 bit integers so it is size * 64.
Example
iex> ref = :atomics.new(3, signed: false)
iex> ref |> Abit.bit_count
192
bit_position(bit_index)
View Sourcebit_position(non_neg_integer()) :: {non_neg_integer(), non_neg_integer()}
Returns a 2 tuple containing:
atomics_index
- the index of the atomics array where the bit is located
bit_index
- the index of the bit in the integer at atomics_index
Example
iex> Abit.bit_position(0)
{1, 0}
iex> Abit.bit_position(11)
{1, 11}
iex> Abit.bit_position(64)
{2, 0}
hamming_distance(ref_l, ref_r)
View Sourcehamming_distance(reference(), reference()) :: non_neg_integer()
Returns the bitwise hamming distance of two :atomics
references.
It accepts two :atomics
references ref_l
and ref_r
.
Raises ArgumentError if the size of ref_l
and ref_r
don't 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.
After the operation ref_a
will be returned.
Bit merge atomics using Bitwise OR operator.
ref_b
will be merged into ref_a
.
After the operation ref_a
will be returned.
set_bit(ref, bit_index, bit)
View Sourceset_bit(reference(), non_neg_integer(), 0 | 1) :: :ok
Sets the bit at bit_index
to bit
in the atomic ref
.
Example
iex> ref = :atomics.new(1, signed: false)
iex> ref |> :atomics.put(1, 1)
iex> ref |> :atomics.get(1)
1
iex> ref |> Abit.set_bit(0, 0)
:ok
iex> ref |> :atomics.get(1)
0
set_bits_count(ref)
View Sourceset_bits_count(reference()) :: non_neg_integer()
Returns number of bits set to 1 in atomics array ref
.
Example
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