itsy v0.0.1 Itsy.Bit

Summary

Functions

Count the set bits

Get the highest set bit

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

Create a mask for the range that exceeds the value. Produces a mask opposite to mask/1

Macros

A guard expression that checks whether the integer is a power of 2 or not

Functions

count(x)

Specs

count(non_neg_integer) :: non_neg_integer

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
highest_set(x)

Specs

highest_set(integer) :: integer

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
lowest_set(x)

Specs

lowest_set(integer) :: integer

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
lowest_unset(x)

Specs

lowest_unset(integer) :: integer

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
mask(x)

Specs

mask(integer) :: integer

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 }
mask_lower_power_of_2(x)

Specs

mask_lower_power_of_2(integer) :: integer

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
min_power_of_2(x)

Specs

min_power_of_2(integer) :: integer

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
power_of_2?(x)

Specs

power_of_2?(integer) :: boolean

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
set(n)

Specs

set(non_neg_integer) :: non_neg_integer

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
unmask(x)

Specs

unmask(integer) :: integer

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 }

Macros

is_power_of_2(x)

Specs

is_power_of_2(term, integer) :: Macro.t

A guard expression that checks whether the integer is a power of 2 or not.