itsy v0.0.2 Itsy.Bit
Link to this section Summary
Functions
Count the set bits
Get the highest set bit
A guard expression that checks whether the integer is a power of 2 or not
Get the lowest set bit
Get the lowest unset bit
Create a mask for the entire value
Masks the lower bits of a power of 2 value. Value must be a power of 2
Find the minimum power of 2 that is equal or greater than the value
Check whether the value is a power of 2
Sets n
least significant bits
Link to this section Functions
Count the set bits.
iex> Itsy.Bit.count(0b1101)
3
iex> Itsy.Bit.count(0b1100)
2
iex> Itsy.Bit.count(0x1feffffffffffffffffffffffffffffffffffffffffffffff)
192
iex> Itsy.Bit.count(0x1feff00000000000000000000000000000000000000000000)
16
Get the highest set bit.
iex> Itsy.Bit.highest_set(0b1101)
0b1000
iex> Itsy.Bit.highest_set(0b1100)
0b1000
iex> Itsy.Bit.highest_set(0x1feffffffffffffffffffffffffffffffffffffffffffffff)
0x1000000000000000000000000000000000000000000000000
iex> Itsy.Bit.highest_set(0x1feff00000000000000000000000000000000000000000000)
0x1000000000000000000000000000000000000000000000000
iex> Itsy.Bit.highest_set(-7)
4
iex> Itsy.Bit.highest_set(-8)
8
A guard expression that checks whether the integer is a power of 2 or not.
Get the lowest set bit.
iex> Itsy.Bit.lowest_set(0b1101)
0b0001
iex> Itsy.Bit.lowest_set(0b1100)
0b0100
iex> Itsy.Bit.lowest_set(0x1feffffffffffffffffffffffffffffffffffffffffffffff)
0x0000000000000000000000000000000000000000000000001
iex> Itsy.Bit.lowest_set(0x1fefffffffffffffffffffffffffffffffffffffffffffffe)
0x0000000000000000000000000000000000000000000000002
iex> Itsy.Bit.lowest_set(-7)
1
iex> Itsy.Bit.lowest_set(-8)
8
Get the lowest unset bit.
iex> Itsy.Bit.lowest_unset(0b1101)
0b0010
iex> Itsy.Bit.lowest_unset(0b1100)
0b0001
iex> Itsy.Bit.lowest_unset(0x1feffffffffffffffffffffffffffffffffffffffffffffff)
0x0010000000000000000000000000000000000000000000000
iex> Itsy.Bit.lowest_unset(0x1fefffffffffffffffffffffffffffffffffffffffffffffe)
0x0000000000000000000000000000000000000000000000001
iex> Itsy.Bit.lowest_unset(-7)
2
iex> Itsy.Bit.lowest_unset(-8)
1
Create a mask for the entire value.
An interesting property of this function is that a positive integer produces a mask that is the opposite to a negative integer.
iex> Itsy.Bit.mask(0b1101)
0b1111
iex> Itsy.Bit.mask(0b1100)
0b1111
iex> Itsy.Bit.mask(0x1feffffffffffffffffffffffffffffffffffffffffffffff)
0x1ffffffffffffffffffffffffffffffffffffffffffffffff
iex> Itsy.Bit.mask(0x1feff00000000000000000000000000000000000000000000)
0x1ffffffffffffffffffffffffffffffffffffffffffffffff
iex> Itsy.Bit.mask(-3)
-4
iex> Itsy.Bit.mask(-4)
-8
iex> :erlang.band(0b1111, Itsy.Bit.mask(2)) + :erlang.band(0b1111, Itsy.Bit.mask(-2))
0b1111
iex> { :erlang.band(0b1111, Itsy.Bit.mask(2)), :erlang.band(0b1111, Itsy.Bit.mask(-2)) }
{ 3, 12 }
Masks the lower bits of a power of 2 value. Value must be a power of 2.
iex> Itsy.Bit.mask_lower_power_of_2(0b1101)
** (Itsy.PowerOf2Error) value 13 is not a power of 2
iex> Itsy.Bit.mask_lower_power_of_2(0b1000)
0b0111
iex> Itsy.Bit.mask_lower_power_of_2(0x10e0000000000000000000000000000000000000000000000)
** (Itsy.PowerOf2Error) value 6620380736540639868108059157289335673232953007833161400320 is not a power of 2
iex> Itsy.Bit.mask_lower_power_of_2(0x1000000000000000000000000000000000000000000000000)
0x0ffffffffffffffffffffffffffffffffffffffffffffffff
Find the minimum power of 2 that is equal or greater than the value.
iex> Itsy.Bit.min_power_of_2(0b1101)
0b10000
iex> Itsy.Bit.min_power_of_2(0b1000)
0b1000
iex> Itsy.Bit.min_power_of_2(0x1feffffffffffffffffffffffffffffffffffffffffffffff)
0x2000000000000000000000000000000000000000000000000
iex> Itsy.Bit.min_power_of_2(0x1000000000000000000000000000000000000000000000000)
0x1000000000000000000000000000000000000000000000000
iex> Itsy.Bit.min_power_of_2(-7)
-8
iex> Itsy.Bit.min_power_of_2(-8)
-8
Check whether the value is a power of 2.
iex> Itsy.Bit.power_of_2?(0b1101)
false
iex> Itsy.Bit.power_of_2?(0b1000)
true
iex> Itsy.Bit.power_of_2?(0x10e0000000000000000000000000000000000000000000000)
false
iex> Itsy.Bit.power_of_2?(0x1000000000000000000000000000000000000000000000000)
true
Sets n
least significant bits.
iex> Itsy.Bit.set(0)
0
iex> Itsy.Bit.set(4)
0b1111
iex> Itsy.Bit.set(70)
0b1111111111111111111111111111111111111111111111111111111111111111111111
iex> Itsy.Bit.set(192)
0xffffffffffffffffffffffffffffffffffffffffffffffff
Create a mask for the range that exceeds the value. Produces a mask opposite to mask/1
.
An interesting property of this function is that a positive integer produces a mask that is the opposite to a negative integer.
iex> Itsy.Bit.unmask(0b1101)
-0b10000
iex> Itsy.Bit.unmask(0b1100)
-0b10000
iex> Itsy.Bit.unmask(0x1feffffffffffffffffffffffffffffffffffffffffffffff)
-0x2000000000000000000000000000000000000000000000000
iex> Itsy.Bit.unmask(0x1feff00000000000000000000000000000000000000000000)
-0x2000000000000000000000000000000000000000000000000
iex> Itsy.Bit.unmask(-3)
3
iex> Itsy.Bit.unmask(-4)
7
iex> :erlang.band(0b1111, Itsy.Bit.unmask(2)) + :erlang.band(0b1111, Itsy.Bit.unmask(-2))
0b1111
iex> { :erlang.band(0b1111, Itsy.Bit.unmask(2)), :erlang.band(0b1111, Itsy.Bit.unmask(-2)) }
{ 12, 3 }