Negative binary numbers
The computer understands only bits. So, this means, the computer can understand all numbers such as integers and knows a way way to represent the integers in some bit format.
The bit format is called a binary number. Binary numbers are fairly easy to understand for a positive integer(0 -> infinity). However, they are not quite straight forward to understand if we talk about negative integers(negative infinity -> -1).
Representation of positive integers in binary
Lets assume that computer can store maximum 4 bits. That means it can store the following combination using 4 bits
0000
0001
0010
0011
0100
0101
0111
1111
I hope the above combinations are the only possible binary representation for 4 bit computer. Now, let us understand what the each of the above binary numbers mean to us(as humans only understand the decimal numbers(-infinty to +infinity :P).
So, there is a very simple formula to convert binary numbers to human readable decimals numbers.
Let us say we to convert binary number ABCD(All A,B,C,D could be either 0 or 1) to decimal.
Formula = (A X (2³)) + (B X (2²)) + (C X (2¹)) + (D X (2⁰))
Some example to understand —
Question 1 : Convert 0010 to binary
(0 X (2³)) + (0 X (2²)) + (1 X (2¹)) + (0 X (2⁰)) = 2
I hope the formula is clear. If not go through again :)
Now, how to represent the negative number in binary. Did you know -267 is represented as 1111111011110101.
I completely lost it when I got to know this because to me
1111111011110101 in decimal is
(1 X (2¹⁵)) + (1 X (2¹⁴)) + (1 X (2¹³)) + (1 X (2¹²)) + (1 X (2¹¹)) + (1 X (2¹⁰)) + (1 X (2⁹)) + (0 X (2⁸)) + (1 X (2⁷)) + (1 X (2⁶)) + (1 X (2⁵)) + (1 X (2⁴)) + (0 X (2³)) + (1 X (2²)) + (0 X (2¹)) + (1 X (2⁰))
which is 65269 if you will calculate.
Now, if you see a binary number in a computer with a MSB(the first bit from left of binary number) as a 1 then assume that it is negative integer.
Get Shivam Gupta’s stories in your inbox
Join Medium for free to get updates from this writer.
So, we can say that 1111111011110101 is a negative number. Now, how can we find out which negative number is it. For that you need to find out the reverse 2’s complement of this binary number.
What is 2' s complement of a binary number ?
- Reverse the binary number. For example — 1010 -> 0101
- Add a 1 to the LSB of reversed binary obtained in the Step 1.
0101
+ 1
— — — —
0110
— — — —
Now, in order to find the decimal represent of 1111111011110101, follow the above in reverse process.
- Subtract 1 from 1111111011110101
1111111011110101
— 1
— — — — — — — — —
1111111011110100
— — — — — — — — —
2. Now, reverse the number obtained in step 2(find out 1’s complement(flip all the bits))
1111111011110100 => 0000000100001011
Now, if you convert 0000000100001011 to decimal, it will come out to be 267. And, as we know that 1111111011110101 is a negative number, it means it is -267 to a computer.




