Two’s complement is a system that allows a binary number to be reversibly converted into a number with an equivalent value but negative.

The process

  1. Find the positive binary value for the negative number you are looking to represent.
  2. Add a leading 0 to the front of the number to show it is positive.
  3. Invert each bit in the number
  4. Add 1 to the number


Denary number: 10 Binary Equivalent: 1010




Result: 10110

Python representation

A fairly rudimentary representation of this algorithmically is as follows. It isn’t perfect and has flaws - but it does the job a decent chunk of the time. Issues are included in the comments.

def FindComplement(inValue):
    # Expect the input to be a positive binary integer
    inValue = "0" + inValue
   # Invert each bit
    inValue = list(inValue)
    outValue = []
    for bit in inValue:
        if bit == "0":
            bit = "1"
        elif bit == "1":
            bit = "0"
        outValue += bit
    # Add 1 to the number
    if outValue[-1] == "0":
        outValue[-1] = "1"
    # Only works if the number is even. Not perfect.
    print("Two's complement is: "+str(outValue)) 
inV = input("Enter binary number to complement: ")