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:
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:
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:
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.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Why are negative numbers stored as 2's complement?
- 8085 program to add three 16 bit numbers stored in registers
- 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 Random Access Memory (RAM) and Content Addressable Memory (CAM)
- Difference between Virtual memory and Cache memory
- Difference between Uniform Memory Access (UMA) and Non-uniform Memory Access (NUMA)
- Representation of Negative Binary Numbers
- Cache Memory in Computer Organization
- Classification and Programming of Read-Only Memory (ROM)
- Different Types of RAM (Random Access Memory )
- Magnetic Tape memory
- 8085 program to exchange a block of bytes in memory
- Hard Disk Drive (HDD) Secondary memory
- Memory Segmentation in 8086 Microprocessor
- Direct memory access with DMA controller 8257/8237
- Memory mapped I/O and Isolated I/O
- Introduction of Secondary Memory
- One bit memory cell (or Basic Bistable element)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.