pak.util.bits

Utilities related to binary operations.

bit(n)

Gets the number with the n-th bit set.

Parameters:

n (int) – The bit to set.

Returns:

The number with the n-th bit set.

Return type:

int

Examples

>>> import pak
>>> pak.util.bit(0)
1
>>> pak.util.bit(1)
2
>>> pak.util.bit(2)
4
to_signed(num, *, bits)

Converts a number to its signed counterpart.

The two’s complement representation of integers is used.

See also

to_unsigned()

Parameters:
  • num (int) – The number to convert.

  • bits (int) – The number of bits to use for the conversion.

Returns:

The signed counterpart of num.

Return type:

int

Examples

>>> import pak
>>> pak.util.to_signed(2**32 - 1, bits=32)
-1
>>> pak.util.to_signed(2**64 - 1, bits=64)
-1
>>> pak.util.to_signed(1, bits=32)
1
to_unsigned(num, *, bits)

Converts a number to its unsigned counterpart.

The two’s complement representation of integers is used.

See also

to_signed()

Parameters:
  • num (int) – The number to convert.

  • bits (int) – The number of bits to use for the conversion.

Returns:

The unsigned counterpart of num.

Return type:

int

Examples

>>> import pak
>>> pak.util.to_unsigned(-1, bits=32)
4294967295
>>> pak.util.to_unsigned(-1, bits=64)
18446744073709551615
>>> pak.util.to_unsigned(1, bits=32)
1