Abit.Bitmask (Abit v1.0.0)

Copy Markdown View Source

Functions for working with bits and integer bitmasks.

set_bits_count/1 and hamming_distance/2 treat integers as 64-bit bitmasks: they inspect bits 0 through 63 and ignore higher bits. Negative integers are interpreted using their lowest 64 two's-complement bits.

The indexed operations are not capped at 64 bits, and to_list/2 uses the explicit size supplied by the caller.

Summary

Functions

Returns the bit at bit_index in the given integer.

Returns the bitwise Hamming distance between the given integers int_l and int_r.

Sets the bit at bit_index in integer to bit.

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

Converts the given integer to a list of bits.

Toggles the bit at bit_index in integer.

Functions

bit_at(integer, bit_index)

@spec bit_at(integer(), non_neg_integer()) :: 0 | 1

Returns the bit at bit_index in the given integer.

Examples

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

hamming_distance(int_l, int_r)

@spec hamming_distance(integer(), integer()) :: non_neg_integer()

Returns the bitwise Hamming distance between the given integers int_l and int_r.

The distance covers only the lowest 64 bits. Bits above bit 63 are ignored, and negative integers are interpreted using their lowest 64 two's-complement bits.

Examples

iex> Abit.Bitmask.hamming_distance(1, 1)
0
iex> Abit.Bitmask.hamming_distance(1, 0)
1
iex> Abit.Bitmask.hamming_distance(1, 1023)
9
iex> Abit.Bitmask.hamming_distance(1, 1024)
2

set_bit_at(integer, bit_index, int)

@spec set_bit_at(integer(), non_neg_integer(), 0 | 1) :: integer()

Sets the bit at bit_index in integer to bit.

Examples

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

set_bits_count(int)

@spec set_bits_count(integer()) :: non_neg_integer()

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

Only the lowest 64 bits are counted. Bits above bit 63 are ignored, and negative integers are interpreted using their lowest 64 two's-complement bits.

Examples

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

to_list(integer, size)

(since 0.2.3)
@spec to_list(integer(), pos_integer()) :: [0 | 1]

Converts the given integer to a list of bits.

size specifies the bitstring size to use before converting the integer to a list.

Examples

iex> Abit.Bitmask.to_list(1, 1)
[1]
iex> Abit.Bitmask.to_list(1, 2)
[0, 1]
iex> Abit.Bitmask.to_list(214311324231232211111, 64)
[1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1]

toggle_bit_at(integer, bit_index)

(since 0.4.0)
@spec toggle_bit_at(integer(), non_neg_integer()) :: integer()

Toggles the bit at bit_index in integer.

Examples

iex> Abit.Bitmask.toggle_bit_at(1, 0)
0
iex> Abit.Bitmask.toggle_bit_at(0, 0)
1
iex> Abit.Bitmask.toggle_bit_at(1, 2)
5