abit v0.1.4 Abit.Bitmask View Source

Functions for working with bits & integer bitmasks.

Link to this section Summary

Functions

Returns bit at bit_index in ingteger.

Returns the hamming distance of the bits of two integers.

Sets the bit at bit_index in integer and returns integer with the bit set.

Returns the count of bits set to 1 in int integer.

Link to this section Functions

Link to this function

bit_at(integer, bit_index)

View Source
bit_at(integer(), non_neg_integer()) :: 0 | 1

Returns bit at bit_index in ingteger.

Example

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 Source
hamming_distance(integer(), integer()) :: non_neg_integer()

Returns the hamming distance of the bits of two integers.

Example

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 Source
set_bit_at(integer(), non_neg_integer(), 0 | 1) :: integer()

Sets the bit at bit_index in integer and returns integer with the bit set.

Example

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 Source
set_bits_count(integer()) :: non_neg_integer()

Returns the count of bits set to 1 in int integer.

Example

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