NaturalSet v0.2.0 BitOps View Source
BitOps provides bit-level operations on integers.
This is a low level utility module to support NaturalSet
.
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
Specs
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
Specs
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
Return a list of all indexes with bit value 1
.
Examples
iex> BitOps.list_ones(0)
[]
iex> BitOps.list_ones(0b1011)
[0, 1, 3]
Specs
Set the bit at index
to 1
.
Examples
iex> BitOps.set_bit(0b101, 1)
7
iex> BitOps.set_bit(0, 8)
256
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 |> Enum.reverse
[7, 5, 3, 2, 1]
Specs
Set the bit at index
to 0
.
Examples
iex> BitOps.unset_bit(0b101, 0)
4
iex> BitOps.unset_bit(0b111, 2)
3