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

# What is an Integer?

Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For example, `0`

, `-5`

, `10.`

The size of `int`

is usually 4 bytes (32 bits). And, it can take `232`

distinct states from `-2147483648`

to `2147483647`

.

*Example.*

**int a = 456;**

The value is 456. Now let us convert it to binary.

256+0+64+32+0+0+4+0+0

1 0 1 1 0 0 1 0 0

Now you get 9 bit number. Since int allocates 32 bits, fill the remaining 23 bits with 0.

So the value stored in memory is

00000000 00000000 00000001 01100100

# Signed and Unsigned.

In C, `signed`

and `unsigned`

are type modifiers. You can alter the data storage of a data type by using them.

signed int a=2357;RHS value is 2357. Not let us convert it to binary.

2048+0+0+256+0+0+32+16+0+4+0+1

1 0 0 1 0 0 1 1 0 1 0 1

Now you get 12 bit number. Since int allocates 32 bits, fill the remaining 23 bits with 0.So the value stored in memory is00000000 00000000 00001001 00110101

For example.

`unsigned int x;`

int y;

Here, the variable x can hold only zero and positive values because we have used the `unsigned`

modifier.

Considering the size of `int`

is 4 bytes, variable y can hold values from `-231`

to `231-1`

, whereas variable x can hold values from `0`

to `232-1`

.

# Store Data in the computer.

Every data store in a computer is store in binary, because is the only language that computers understand. Other languages like decimal are interpretation, and the machine has to translate it, to binary to really understand. For instance, the number 4 is 100 in binary, so the memory would be something like (4 bytes):

00000000 00000000 00000000 000001000x0A 0x0B 0x0C 0x0D memory address

The C standard doesn’t mandate any particular way of representing **negative signed numbers.**

In most implementations that you are likely to encounter, negative signed integers are stored in what is called **two’s complement**. The other major way of storing negative signed numbers is called **one’s complement**.

The two’s complement of an N-bit number `x`

is defined as `2^N - x`

. For example, the two's complement of 8-bit `1`

is `2^8 - 1`

, or `1111 1111`

. The two's complement of 8-bit `8`

is `2^8 - 8`

, which in binary is `1111 1000`

. This can also be calculated by flipping the bits of `x`

and adding one.

*For example.*

`1 = 0000 0001`

~1 = 1111 1110 (1's complement)

~1 + 1 = 1111 1111 (2's complement)

-1 = 1111 1111

21 = 0001 0101

~21 = 1110 1010

~21 + 1 = 1110 1011

-21 = 1110 1011

*The one’s complement of an N-bit number x is defined as x with all its bits flipped, basically.*

`1 = 0000 0001`

-1 = 1111 1110

21 = 0001 0101

-21 = 1110 1010

Two’s complement has several advantages over one’s complement. For example, it doesn’t have the concept of ‘negative zero’, which for good reason is confusing to many people. Addition, multiplication and subtraction work the same with signed integers.

# Signed magnitude.

This is the easiest to understand, because it works the same as we are used to when dealing with negative decimal values: The first position (bit) represents the sign (0 for positive, 1 for negative), and the other bits represent the number. Although it is easy for us to understand, it is hard for computers to work with, especially when doing arithmetic with negative numbers.

In 8-bit signed magnitude, the value 8 is represented as 0 0001000 and -8 as 1 0001000.

# One’s complements

In this representation, negative numbers are created from the corresponding positive number by flipping all the bits and not just the sign bit. This makes it easier to work with negative numbers for a computer, but has the complication that there are two distinct representations for +0 and -0. The flipping of all the bits makes this harder to understand for humans.

In 8-bit one’s complement, the value 8 is represented as 00001000 and -8 as 11110111.

# Two’s complements

This is the most common representation used nowadays for negative integers because it is the easiest to work with for computers, but it is also the hardest to understand for humans. When comparing the bit patterns used for negative values between one’s complement and two’s complement, it can be observed that the same bit pattern in two’s complement encodes for the next lower number. For example 11111111 stands for -0 in one’s complement and for -1 in two’s complement, and similarly for 10000000 (-127 vs -128).

In 8-bit two’s complement, the value 8 is represented as 00001000 and -8 as 11111000.

# EXAMPLES.

Suppose the following fragment of code, int a = -34; Now how will this be stored in memory. So here is the complete theory. 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.

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.

Int number : -4

Binary representation (no sign): 100

Binary representation in memory (no sign): 00000000 00000000 00000000 00000100Now we add the sign using 2’s complement:1 step: flip — 11111111 11111111 11111111 111110112 step: add 1–11111111 11111111 11111111 11111100

If you want more information about this please click *here**. *I hop this blog can helps you!