The goal is to write a pair of functions the first of which will take a string of binary along with a specification of bits, which will return a numeric, signed complement in two's complement format. The second will do the reverse. It will take in an integer along with a number of bits, and return a binary string. 

https://en.wikipedia.org/wiki/Two's_complement

Thus, to_twos_complement should take the parameters binary = "0000 0001", bits = 8 should return 1. And, binary = "11111111", bits = 8 should return -1 . While, from_twos_complement should return "00000000" from the parameters n = 0, bits = 8 . And, "11111111" from n = -1, bits = 8. 

You should account for some edge cases.

Difficulty: introductory

Starter code:

def to_twos_complement(binary, bits):
	

Solutions:
def to_twos_complement(binary, bits):
    return int(binary.replace(' ', ''), 2) - 2 ** bits * int(binary[0])

def from_twos_complement(n, bits):
    return '{:0{}b}'.format(n & 2 ** bits - 1, bits)

def to_twos_complement(b, bits):
    b = b.replace(" ","")
    return int(b,2) if int(b,2) < 2**(bits-1) else int(b,2)-2**(bits)

def from_twos_complement(n, bits):
    return bin(2**bits+n)[2:].zfill(bits) if n<0 else bin(n)[2:].zfill(bits)

def to_twos_complement(input_value, bits):
    mask, unsigned = 2 ** (bits - 1), int(input_value.replace(' ', ''), 2)
    return (unsigned & ~mask) - (unsigned & mask)

def from_twos_complement(n, bits):
    return "{:0>{}b}".format(n + 2 ** bits * (n < 0), bits)

def to_twos_complement(binary, bits):
    return int(binary.replace(' ', ''), 2) - (2 ** bits) * binary.startswith('1')
        
def from_twos_complement(n, bits):
    return '{:0{}b}'.format((n + 2 ** bits) % (2 ** bits), bits)

def to_twos_complement(binary, bits): 
    conv = int(binary.replace(' ',''), 2)
    return conv - (conv >= 2**bits/2) * 2**bits

def from_twos_complement(n, bits):
    return ("{:0>"+str(bits)+"b}").format(n + (2**bits) * (n<0))

to_twos_complement=lambda s,l:int(s.replace(' ',''),2)if s[0]=='0'else-(int("".join([str(int(i)^1)for i in s.replace(' ','')]),2)+1)
from_twos_complement=lambda n,b:bin(n&0xffffffffffffffffffff)[2:][-b:]if n<0else bin(n)[2:].zfill(b)

def to_twos_complement(binary, bits):
    binary = binary.replace(" ", "")
    chiffre = binary[0]
    n = int(binary, 2)
    if chiffre == '1':
        n -= 2**bits
    return n
def from_twos_complement(n, bits):
    
    if n<0:
        n += 2**bits
        
    chaine = bin(n)
    chaine = chaine[2:]
    chaine = (bits-len(chaine))*'0'+chaine
    return chaine


def to_twos_complement(binary, bits):
    binary="".join(binary.split())
    res= -(2**(bits-1)) if binary[0]=="1" else 0
    for i in range(1, bits):
        res+=2**(bits-1-i) if binary[i]=="1" else 0
    return res
def from_twos_complement(n, bits):
    if n>=0:
        return "0"*(bits-len(bin(n)[2:]))+bin(n)[2:]
    else:
        remain=abs(-(2**(bits-1))-n)
        return "1"+"0"*(bits-len(bin(remain)[2:])-1)+bin(remain)[2:]

def to_twos_complement(binary, bits):
    binary = binary.replace(" ", "")
    sign_bit = binary[0]
    if sign_bit == "1":
        return int(binary[1:], 2) - 2**(bits-1)
    else:
        return int(binary, 2)

def from_twos_complement(n, bits):
    print("new")
    if n >= 0:
        return format(n,'b').zfill(bits)
    else:
        return format(2**bits + n, 'b')

def to_twos_complement(binary, bits):
    x=int(binary.replace(' ',''),2)
    if binary[0]=='0':
        return x
    else:
        return -((x^(2**bits-1))+1)
    

def from_twos_complement(n, bits):
    if n>=0:
        return '{:b}'.format(n).zfill(bits)
    else:
        return '{:b}'.format(2**bits+n).zfill(bits)