Compared to positive values, the way how negative values get stored in the computer is completely different. Most computers today, use2's complement binary form
to store a negative value. Let’s see how it’ll work.
![](https://paperbun.org/content/images/2021/08/how_negative_numbers_stored_in_a_variable_1.jpeg)
consider, here I am storing -27
in this variable.
The equivalent binary of -27
is the 2’s complement binary of its absolute value.
To get the absolute value of -27, just omit its sign i.e. you will get 27
as an absolute value.
![](https://paperbun.org/content/images/2021/08/2_s_complementary_binary.png)
To get 2’s complement binary value
- Find the equivalent binary of the absolute value.
- Find the one’s complement to the result of step1
- Add one to the 1’s complement binary result to get 2’s complement binary.
For example, here the absolute value of -27 is 27.
Step 1: The binary value of 27 is
![](https://paperbun.org/content/images/2021/08/2_s_complimentary_step_1.png)
Step 2: Find the one’s complement of this binary value.
![](https://paperbun.org/content/images/2021/08/2_s_complimentary_step_2.png)
Step 3: Add 1
to the one’s complement binary value, you will get the below two’s complement result.
![](https://paperbun.org/content/images/2021/08/2_s_complimentary_step_3.png)
This is the value that will get stored in the variable’s memory space.