View Source Abit.Bitmask (abit v0.3.3)
Functions for working with bits & integer bitmasks.
Summary
Functions
Returns bit at bit_index
in the given integer
.
Returns the bitwise Hamming distance between the
given integers int_l
and int_r
.
Sets the bit at bit_index
in integer
and returns it.
Returns the count of bits set to 1 in the given integer int
.
Converts the given integer
to a list of bits.
Functions
@spec bit_at(integer(), non_neg_integer()) :: 0 | 1
Returns bit at bit_index
in the given integer
.
Examples
iex> Abit.Bitmask.bit_at(2, 0)
0
iex> Abit.Bitmask.bit_at(2, 1)
1
iex> Abit.Bitmask.bit_at(1, 0)
1
iex> Abit.Bitmask.bit_at(0, 0)
0
@spec hamming_distance(integer(), integer()) :: non_neg_integer()
Returns the bitwise Hamming distance between the
given integers int_l
and int_r
.
Examples
iex> Abit.Bitmask.hamming_distance(1, 1)
0
iex> Abit.Bitmask.hamming_distance(1, 0)
1
iex> Abit.Bitmask.hamming_distance(1, 1023)
9
iex> Abit.Bitmask.hamming_distance(1, 1024)
2
@spec set_bit_at(integer(), non_neg_integer(), 0 | 1) :: integer()
Sets the bit at bit_index
in integer
and returns it.
Examples
iex> Abit.Bitmask.set_bit_at(1, 0, 0)
0
iex> Abit.Bitmask.set_bit_at(0, 0, 1)
1
iex> Abit.Bitmask.set_bit_at(0, 2, 1)
4
@spec set_bits_count(integer()) :: non_neg_integer()
Returns the count of bits set to 1 in the given integer int
.
Examples
iex> Abit.Bitmask.set_bits_count(3)
2
iex> Abit.Bitmask.set_bits_count(0)
0
iex> Abit.Bitmask.set_bits_count(1024)
1
iex> Abit.Bitmask.set_bits_count(1023)
10
@spec to_list(integer(), pos_integer()) :: [0 | 1]
Converts the given integer
to a list of bits.
size
is the size of the bitstring you want the integer to be
converted to before creating a list from it.
Examples
iex> Abit.Bitmask.to_list(1, 1)
[1]
iex> Abit.Bitmask.to_list(1, 2)
[0, 1]
iex> Abit.Bitmask.to_list(214311324231232211111, 64)
[1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1]