How integers are stored in memory using two’s complement.

But, what’s an integer?

Bit what?

Now, a bit is the most basic unit of the computer memory. Going back to Wikipedia: “The bit is a basic unit of information in computing and digital communications. The name is a portmanteau of binary digit. The bit represents a logical state with one of two possible values. These values are most commonly represented as either “1”or”0", but other representations such as true/false, yes/no, +/−, or on/off are common.

00000000 00000000 00000000 00110010

But what happens when representing negative integers such as -50?

10000000 00000000 00000000 00110010

The problem is that using MSB to represent negative numbers has some important disadvantages, starting with the fact that reserving one bit to represent the negative/positive value of the number reduces the amount of available numbers by one half.

10000000 00000000 00000000 00000000

Enters one’s complement:

A possible work around to the reduction of the range of numbers is to utilize the method called one’s complement.

What about two’s complement then?

Wikipedia (again): “Compared to other systems for representing signed numbers (e.g., ones’ complement), two’s complement has the advantage that the fundamental arithmetic operations of addition, subtraction, and multiplication are identical to those for unsigned binary numbers (as long as the inputs are represented in the same number of bits as the output, and any overflow beyond those bits is discarded from the result). This property makes the system simpler to implement, especially for higher-precision arithmetic. Unlike ones’ complement systems, two’s complement has no representation for negative zero, and thus does not suffer from its associated difficulties.

But how?

In a nutshell:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store