dolor.util.bits¶
Utilities related to binary bits.
- bit(n)[source]¶
- Parameters
n (
int) – The bit to set.- Returns
The number with the nth bit set.
- Return type
int
Examples
>>> import dolor >>> dolor.util.bit(0) 1 >>> dolor.util.bit(1) 2 >>> dolor.util.bit(2) 4
- to_signed(val, *, bits=32)[source]¶
Converts a number to its signed counterpart.
- Parameters
val (
int) – The value to convert.bits (
int, optional) – How many bits to use when convertingvalto its signed counterpart.
- Returns
val’s signed counterpart.- Return type
int
Examples
>>> import dolor >>> dolor.util.to_signed(2**32 - 1) -1 >>> dolor.util.to_signed(2**64 - 1, bits=64) -1
- to_unsigned(val, *, bits=32)[source]¶
Converts a number to its unsigned counterpart.
- Parameters
val (
int) – The value to convert.bits (
int, optional) – How many bits to use when convertingvalto its unsigned counterpart.
- Returns
val’s unsigned counterpart.- Return type
int
Examples
>>> import dolor >>> dolor.util.to_unsigned(-1) 4294967295 >>> dolor.util.to_unsigned(-1, bits=64) 18446744073709551615
- urshift(val, n, *, bits=32)[source]¶
Performs an unsigned right shift on a number.
- Parameters
val (
int) – The value to shift.n (
int) – How many bits to shiftval.bits (
int, optional) – How many bits should be used for the “unsigned” part of “unsigned right shift”.
- Returns
The resulting unsigned right shifted number.
- Return type
int
Examples
>>> import dolor >>> dolor.util.urshift(2, 1) 1 >>> dolor.util.urshift(-1, 1) 2147483647 >>> dolor.util.urshift(-1, 1, bits=64) 9223372036854775807