abit v0.2.3 Abit.Bitmask View Source
Functions for working with bits & integer bitmasks.
Link to this section 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.
Link to this section Functions
Link to this function
bit_at(integer, bit_index)
View Sourcebit_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
Link to this function
hamming_distance(int_l, int_r)
View Sourcehamming_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
Link to this function
set_bit_at(integer, bit_index, int)
View Sourceset_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
Link to this function
set_bits_count(int)
View Sourceset_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
Link to this function
to_list(integer, size)
View Source (since 0.2.3)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]