oxy_lc.utilities.conversions
1def twos_compliment(decimal: int, bits: int) -> int: 2 """ 3 Decodes a twos compliment int into its signed counterpart 4 5 :param decimal: The unsigned int to be decoded 6 :type decimal: int 7 :param bits: the range of bits used in the compliment 8 :type bits: int 9 :return: signed integer 10 :rtype: int 11 """ 12 binary_string = f"{decimal:0{bits}b}" 13 14 if binary_string[0] != '0': 15 decimal = decimal - (1 << bits) 16 17 return decimal
def
twos_compliment(decimal: int, bits: int) -> int:
2def twos_compliment(decimal: int, bits: int) -> int: 3 """ 4 Decodes a twos compliment int into its signed counterpart 5 6 :param decimal: The unsigned int to be decoded 7 :type decimal: int 8 :param bits: the range of bits used in the compliment 9 :type bits: int 10 :return: signed integer 11 :rtype: int 12 """ 13 binary_string = f"{decimal:0{bits}b}" 14 15 if binary_string[0] != '0': 16 decimal = decimal - (1 << bits) 17 18 return decimal
Decodes a twos compliment int into its signed counterpart
Parameters
- decimal: The unsigned int to be decoded
- bits: the range of bits used in the compliment
Returns
signed integer