## Two's complement

It's used to represent signed integers. The formula is

$$w = - a_{N - 1}\, 2^{N - 1} + \sum_{i = 0}^{N - 2} a_i\,2^i$$

in practice the MSB has much more weight with respect to the others; This system can represent the range $$[-2^{N - 1}, 2^{N - 1} - 1]$$.

Below a simple python function to calculate the value

def twos_complement(input_value, num_bits):
'''Calculates a two's complement integer from the given input value's bits'''