Horje
python convert integer to signed base 2 complement Code Example
python convert integer to signed base 2 complement
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))




Python

Related
class views django slug Code Example class views django slug Code Example
how to block a ip adress Code Example how to block a ip adress Code Example
http python lib Code Example http python lib Code Example
how to select number by twos in a list python next to each Code Example how to select number by twos in a list python next to each Code Example
python graphql client Code Example python graphql client Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8