#!/usr/bin/python3
# -*- coding: utf-8 -*-

def int2bin(val, negConv = "2"):
    negConvs = {-1: "Valeur absolue unsigned",
                 0: "Bit de signe + valeur absolue",
                 1: "Signed complément à un",
                 2: "Signed complément à deux"}
    if negConv not in negConvs:
        return "Convention de signe inconnue"
    positive = True
    if (val == 0):
        return "0"
    if (val < 0):
        positive = False
        val = -val
            
    bitList = list()
    while (val > 0):
        bitList.append(val % 2)
        val = val // 2
    if positive and negConv >= 0:
        bitList.append(0)
    else:
        if negConv == 0:
            bitList.append(1)
        elif negConv > 0:
            # Complément à 1 : on inverse tous les bits
            bitList = list(map(lambda x: (x + 1) % 2, bitList))
            if negConv == 1:
                # On doit avoir un 1 en première position
                bitList.append(1)
            else:
                # Complément à deux : on ajoute 1 à la valeur complément à 1
                carry = 1
                newBitList = list()
                for bit in bitList:
                    if bit == 1 and carry == 1:
                        newBitList.append(0)
                        carry = 1
                    else:
                        newBitList.append(bit + carry)
                        carry = 0
                if newBitList[len(newBitList) - 1] == 0:
                    # On doit avoir un 1 en première position
                    newBitList.append(1)
                bitList = newBitList
    return "%s :\n%s (%s)" % (negConvs[negConv], "".join(map(str, reversed(bitList))), len(bitList))
val = -33
print("Décimal : %s" % val)
print(int2bin(val, -1))
print(int2bin(val, 0))
print(int2bin(val, 1))
print(int2bin(val, 2))