How integers are stored in memory using two’s complement

Chiara Caprasi
Dev Genius
Published in
3 min readMar 25, 2022

--

Photo credit: log2base2.com

In this short article I will be sharing how integers are stored in the computer memory using two’s complement. If you want to learn more about memory, you can read my previous blog post, Memory in C Programming🔗. So, without further ado, let’s get started!

What’s an Integer?

An integer is a whole number (from the Latin integer meaning “whole”) — that is a number that can be written without a fractional component.

Most programming languages provide a data type called ‘integer’, often called ‘int’ for short. In computer integers are stored using 4 bytes (32 bit) of memory.

In the context of programming, integers are whole numbers represented as binary values.

Binary Numbers

Binary numbers are numbers expressed in the base-2 numeral system or binary numeral system, a method of mathematical expression which uses only two symbols: 0 (zero) and 1 (one).

All computer data is represented using binary, so the digit 0 and 1. A binary digit, or bit, is the smallest unit of data in computing.

Positive and Negative Numbers

Computers need to store both positive and negative numbers. Since there are only two symbols available in binary (0 and 1), we need to find a way to represent the negative and positive sign using 0 or 1.

Two’s complement format is how computer store negative numbers.

In binary number, the MSB (most significant bit), that is the bit furthest to the left, is used to indicate whether the number is positive or negative.

  • In a positive number the MSB will be 0.
  • In a negative number the MSB will be 1.

Let’s take the number 65. Its binary 8–bit representation (char) will be 01000001 or in 32 bit quantity (int) it will look like in the picture below.

Image of 65 in binary in a 32 bit quantity. Photo credit — log2base2.com

Two’s Complement

Computer store the negative value of a number using the 2’s complement. To understand 2’s complement, we need to first look at 1’s complement.

In 1’s complement of a number, we just invert the binary bits of the number — so the 0s become 1s and vice versa.

So, if we stay with 0100 0001(65 in base 10), it’s 1s complement is 1011 1110

To obtain the 2’s complement, we just add 1 to the 1’s complement. So 0–65 it will be 1011 1111 in binary.

This method of inverting and adding one works both ways — if we want to calculate from or to negative/positive.

Let’s have a look at another example — take the decimal number 10

  • Decimal: 10
  • Binary: 0000 1010
  • 1’s Complement: 1111 0101
  • 2’s Complement: 1111 0110

As illustrated in the picture below 👇🏾

I hope you found this article helpful. For any questions, comments or if you would like to just say hello connect with me on LinkedIn or follow me on Medium.

Stay happy and keep coding! 👩🏻‍💻

Resources

--

--

Happy Developer. Passionate about front end, web accessibility and using technology to make a positive change.