bitmath#

Bit-twiddling functions of unsigned integers

Functions

all_bits_are_one(number, bit_indices)

Check if all specified bits in an integer are set to 1.

flip_bit(number, bit_index)

Flip the value of a specific bit in an integer.

flip_bits(number, bit_indices)

Flip the values of multiple specific bits in an integer.

get_bit(number, bit_index)

Get the value of a specific bit in an integer.

get_bit_mask(bit_indices)

Get the integer representation of a bitmask with bits set at the specified indices.

insert_bit(number, bit_index, bit_value)

Insert a bit value at a specific index in an integer.

is_power_of_2(number)

Check if a number is a power of 2.

log_base2(number)

Calculate the base-2 logarithm of a power-of-2 number.

power_of_2(exp)

Calculate 2 raised to the power of the given exponent.

all_bits_are_one(number: int, bit_indices: list[int]) bool[source]#

Check if all specified bits in an integer are set to 1.

flip_bit(number: int | Tensor, bit_index: int) int | Tensor[source]#

Flip the value of a specific bit in an integer.

flip_bits(number: int | Tensor, bit_indices: list[int]) int | Tensor[source]#

Flip the values of multiple specific bits in an integer.

get_bit(number: int | Tensor, bit_index: int) int | Tensor[source]#

Get the value of a specific bit in an integer.

get_bit_mask(bit_indices: list[int]) int[source]#

Get the integer representation of a bitmask with bits set at the specified indices.

insert_bit(number: int | Tensor, bit_index: int, bit_value: int) int | Tensor[source]#

Insert a bit value at a specific index in an integer.

is_power_of_2(number: int) bool[source]#

Check if a number is a power of 2.

log_base2(number: int) int[source]#

Calculate the base-2 logarithm of a power-of-2 number.

power_of_2(exp: int) int[source]#

Calculate 2 raised to the power of the given exponent.