📜  python 将整数转换为带符号的以 2 为底的补码 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:08.414000             🧑  作者: Mango

代码示例1
def twos_comp(val, bits):
    """compute the 2's complement of int value val"""
    if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
        val = val - (1 << bits)        # compute negative value
    return val                         # return positive value as is
  
binary_string = '1010101010' # or whatever... no '0b' prefix
out = twos_comp(int(binary_string,2), len(binary_string))