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

Joudirim
4 min readMar 25, 2021

--

Hello, in this blog we are going to talk about integers and how they are stored in computer memory using two’s complement.

Integers are whole numbers. The range of values that can be stored as an integer depends on whether or not the number is signed (positive or negative), and how much memory is allocated for it in memory.

Programming languages can generally represent integers that are signed or unsigned, and of different sizes.

A single byte can represent unsigned numbers ranging in value from 0 to 255, or signed integers ranging from -128 to +127.

If two bytes are used, unsigned numbers from 0 to 65,535 or signed numbers from -32,768 to 32,767 can be stored. Much larger numbers can be represented if more bytes are made available.

For signed numbers, one bit is used to store the sign (+ or -) of the number, so the absolute value of the biggest number that can be stored is only half that for unsigned numbers. The number of bits used to represent an integer value will equal the number of bytes multiplied by eight.

An integer represented by n bits can represent 2n numbers.

The magnitude of a four-byte integer can thus be anything up to 2 (4×8) or 2 32 which means it can hold an unsigned value of up to 4,294,967,296 (a tad over two billion).

Negative numbers can be represented in several different ways in binary number systems, although the most commonly used method is two’s complement.

Below are the integers 1 to 5 stored as four-byte values (each row represents one integer).

0  :  00000001 00000000 00000000 00000000 |  1
4 : 00000010 00000000 00000000 00000000 | 2
8 : 00000011 00000000 00000000 00000000 | 3
12 : 00000100 00000000 00000000 00000000 | 4
16 : 00000101 00000000 00000000 00000000 | 5

This may look a little strange; within each byte (each block of eight bits), the bits are written from right to left like we are used to in normal decimal notation, but the bytes themselves are written left to right!

1 Byte consists of 8 Bites

It turns out that the computer does not mind which order the bytes are used (as long as we tell the computer what the order is) and most software uses this left to right order for bytes.

How the negative numbers are stored in memory?

Suppose the following fragment of code, int a = -34;

Now how will this be stored in memory ?

Whenever a number with minus sign is encountered, the number (ignoring minus sign) is converted to its binary equivalent.

Then the two’s complement of the number is calculated.

That two’s complement is kept at place allocated in memory and the sign bit will be set to 1 because the binary being kept is of a negative number.

Whenever it comes on accessing that value firstly the sign bit will be checked if the sign bit is 1 then the binary will be two’s complemented and converted to equivalent decimal number and will be represented with a minus sign.

Let us see how it works ;
Example
int a = -2056;
Binary of 2056 will be calculated which is:

00000000000000000000100000001000 (32 bit representation, according of storage of int in C)

2’s complement of the above binary is:
11111111111111111111011111111000.

So finally the above binary will be stored at memory allocated for variable a.

When it comes on accessing the value of variable a, the above binary will be retrieved from the memory location, then its sign bit that is the left most bit will be checked as it is 1 so the binary number is of a negative number so it will be 2’s complemented and when it will be 2’s complemented will be get the binary of 2056 which is:
00000000000000000000100000001000

The above binary number will be converted to its decimal equivalent which is 2056 and as the sign bit was 1 so the decimal number which is being gained from the binary number will be represented with a minus sign. In our case -2056.

So Whats Two’s Complement ?

Because of the limitations of one’s complement when carrying out arithmetic operations such as multiplication and division, the most commonly used way of representing signed integers on computers is to use two’s complement, because it allows the logic that handles arithmetic functions to be implemented more easily in hardware.

The two’s complement of a binary number is the value obtained by subtracting the number from a large power of two (specifically, from 2n for an n-bit number).

As with one’s complement, the most significant bit is used to indicate the sign (0 = positive, 1 = negative), and positive numbers are represented in the same way.

To negate a positive number, its two’s complement is used.

Taking the unsigned number 310 as an example, this would be represented as a positive 8-bit two’s complement binary number as 000000112. The value of 28 expressed in standard binary format is 100000000.

In order to find the two’s complement of +3, therefore, we would carry out the following arithmetic operation:

100000000
- 00000011
— — — —
= 11111101

Negative binary numbers can thus be represented using the two’s complement of their absolute value.

The absolute value of the largest negative number that can be represented with a given number of bits is always one more than that of the largest positive number that can be represented using the same number of bits.

Thus, a two’s complement 8-bit binary number can represent signed integer values from −128 to +127 (note that the two’s complement of -128 is -128).

--

--