Representation of Negative Numbers in Binary
Originally published here.
Since binary numbers can have only two digits/symbols either 0 or 1, it is not possible to add minus or plus symbol in front of a binary number.
In a decimal number, we add a minus symbol in front of a number if it is negative. In computer systems, we represent a negative and positive number by adding an extra bit in front of them called a sign bit. If the bit is 0 then the number is positive else if the bit is 1 then the number is negative.
Ways to represent negative numbers in binary number system
There are several ways to represent negative numbers in binary which include:
Sign and magnitude representation, One's complement representation, and Two's complement method. These are explained below:
1. Sign and Magnitude Representation
In this method, the leftmost bit is used to indicate the sign of a number which is called a sign bit. The value of the sign bit is 1 for negative numbers and 0 for positive numbers. The remaining bits are used to represent the magnitude of the number.
Let's suppose we have a 4-bit number, we can represent the positive number 5 as 0101, and the negative number -5 as 1101. We convert this binary number into its decimal equivalent by performing the binary to decimal conversion we learned in the previous article, but remember to convert the sign bit and apply the appropriate sign to the result.
This method has an ambiguous representation of the number 0, which means 0 has two different representations one is +0 (0000 in 4-bit binary number) and another is -0 (1000 in 4-bit binary number)
2. One's Complement Representation
In order to get One's complement of a binary number, simply invert all the bits of the number. That means converting all the 0's into 1 and all 1's into 0. Let's understand this using an example. Suppose there is a binary number 11000101, then its one's complement will be 00111010.
Always remember that the leftmost bit or MSB (Most Significant Bit) is always 1 for negative numbers. So in order to represent a negative number in binary we take one's complement of that binary number.
This method has also an ambiguous representation of the number 0, which means 0 has two different representations one is +0 (0000 in 4-bit binary number) and another is -0 (1111 in 4-bit binary number)
3. Two's Complement Representation
In order to get Two's complement of a binary number, simply invert all the bits of the number. That means converting all the 0's into 1 and all 1's into 0 and then add 1 to this number. In other words, we can say, take the one's complement of the number and add 1 to it. Let's understand this using an example. Suppose there is a binary number 11000101, then its one's complement will be 00111010. After adding 1 to this number it will become 00111011.
In this method there is only one representation of +0 and -0, so, two's complement representation is better than sign and magnitude representation and one's complement
Subscribe to my newsletter
Read articles from Syed Muzamil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Syed Muzamil
Syed Muzamil
Full Stack Developer at wealthup. I love working with new tech and building stuff. I work with technologies like ReactJs, NodeJs, MongoDB, Flutter and some AWS services.