# How the negative numbers are stored in memory?

Prerequisite – Base conversions, 1’s and 2’s complement of a binary number, 2’s complement of a binary string

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.

Let us take an example:

**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.

## Recommended Posts:

- Why are negative numbers stored as 2's complement?
- 8085 program to add three 16 bit numbers stored in registers
- Difference between Random Access Memory (RAM) and Content Addressable Memory (CAM)
- Difference between Virtual memory and Cache memory
- Random Access Memory (RAM) and Read Only Memory (ROM)
- Introduction to memory and memory units
- Difference between Byte Addressable Memory and Word Addressable Memory
- Difference between Uniform Memory Access (UMA) and Non-uniform Memory Access (NUMA)
- Memory Interleaving
- 2D and 2.5D Memory organization
- Cache Memory Design
- Introduction of Secondary Memory
- Memory Banking in Microprocessor
- Memory mapped I/O and Isolated I/O
- Different Types of RAM (Random Access Memory )

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.