Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

complement
two
signed
number
binary

# Why does two's complement work? Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

## Two’s complement

Two’s complement is a technique for representing signed numbers in binary. The method representing a negative number is a simple two-step process:

• Take the complement of the number’s positive counterpart
• Add $1$

The powerful thing about two’s complement representation is that we can perform arithmetic with the numbers as they are, and the answer we get will be the correct representation in two’s complement. But why does this simple technique work and result in such a mathematically consistent representation?

It is simply the result of subtracting the absolute value of the number from $0.$

Let us see how this is true.

## Subtraction from $0$

Suppose we want to come up with a representation for -$5$ in binary. We know -$5$ is $5$ less than $0$, $0-5$. We can try and compute this using the borrow method​ if we infinitely keep borrowing from the $0$ on the left. Although our answer will have a never-ending string of $1$'s at its left, we can simply truncate after $8$ bits because that is all our computer will store – so this is our negative representation.

Now let’s follow the two’s complement method to find -$5$. This is exactly the same as subtracting from $0$ using the borrow method. Now you know what two’s complement does.

RELATED TAGS

complement
two
signed
number
binary 