UintSet v0.1.3 BitOps View Source
BitOps provides bit-level operations on integers.
This is a low level utility module to support UintSet.
All the functions take an integer as the first argument;
most return new integers by flipping bits on the first argument,
returning the value of a specific bit, or finding bits set to 1.
Link to this section Summary
Functions
Count the bits of value 1 in an integer.
Get the value of the bit at the index.
Return a list of all indexes with bit value 1.
Set the bit at index to 1.
Returns a stream function yielding the indexes with bit value 1.
The stream lazily traverses the bits of the integer as needed.
Set the bit at index to 0.
Link to this section Functions
count_ones(bigint)
View Source
count_ones(non_neg_integer()) :: non_neg_integer()
count_ones(non_neg_integer()) :: non_neg_integer()
Count the bits of value 1 in an integer.
Examples
iex> BitOps.count_ones(0)
0
iex> BitOps.count_ones(3)
2
iex> BitOps.count_ones(0b111_0000_1111)
7
get_bit(bigint, index) View Source
Get the value of the bit at the index.
Examples
iex> BitOps.get_bit(0b101, 0)
1
iex> BitOps.get_bit(0b101, 1)
0
iex> BitOps.get_bit(0b101, 99)
0
list_ones(bigint) View Source
Return a list of all indexes with bit value 1.
Examples
iex> BitOps.list_ones(0)
[]
iex> BitOps.list_ones(0b1011)
[0, 1, 3]
set_bit(bigint, index) View Source
Set the bit at index to 1.
Examples
iex> BitOps.set_bit(0b101, 1)
7
iex> BitOps.set_bit(0, 8)
256
stream_ones(bigint) View Source
Returns a stream function yielding the indexes with bit value 1.
The stream lazily traverses the bits of the integer as needed.
Examples
iex> my_stream = BitOps.stream_ones(0b1010_1110)
iex> my_stream |> is_function
true
iex> my_stream |> Stream.map(&(&1 * 10)) |> Enum.to_list
[10, 20, 30, 50, 70]
unset_bit(bigint, index) View Source
Set the bit at index to 0.
Examples
iex> BitOps.unset_bit(0b101, 0)
4
iex> BitOps.unset_bit(0b111, 2)
3