Why Negative numbers are stored in two’s complement form in computer
We know numbers can be represented in magnitude form (Normal binary representation) , one’s complement form and two’s complement form but why we don’t store number in magnitude form or one’s complement form in computer and why we store number in two’s complement form in computer will see here.
In digital, Number representation is classified as follows:
In magnitude representation, Number is represented as the same way, the way we represent in normal Binary representation. Assuming you know at least how to represent numbers in Binary number system.
example: Binary representation of some decimal numbers is given below.
510=01012
1010=10102
1510=11112
Magnitude representation is classified in two types namely Magnitude unsigned representation and magnitude signed representation.
Magnitude unsigned representation used to represent only positive numbers. In this representation positive numbers are represented as normal binary representation. As this representation use to represent only positive numbers there is no need of sign bit. Sign bit basically use to differentiate between positive and negative numbers.
Example: Magnitude unsigned representation of some decimal numbers is given below.
410=01002
810=10002
1610=100002
Magnitude signed representation use to represent positive as well as negative numbers. In magnitude signed representation MSB bit represents Sign bit and rest bits are binary representation of number . MSB bit 0 represents positive number and MSB bit 1 represent negative number.
example: Magnitude signed representation of decimal numbers.
510=01012 /*MSB 0 is sign bit representing positive number and 101 is binary representation of 5*/
-510=11012 /*MSB 1 is sign bit representing negative number and 101 is binary representation of 5*/
Problem with Magnitude signed representation: We all know there is nothing like positive zero or negative zero. In this representation there are two different representations for zero.
example:
+0=0000 /*Positive zero representation*/
-0=1000 /*Negative zero representation*/
Practically there should only one representation for zero but there are two representations for zero that why we don’t use magnitude signed number representation in Computer.
Complement representation classified as one’s complement representation and two’s complement representation.
One’s complement representation of binary number is obtained by inverting all zero’s by one’s and vice versa.
Example:
one’s complement of 1010 is 0101
one’s complement of 0000 is 1111
One’s complement representation classified as Unsigned one’s complement and Signed one’s complement.
In all number representation types whether it’s magnitude or complement number representation, positive numbers always represented as magnitude unsigned binary representation.
Example:
1010=10102
510=01012
Signed one’s complement representation use to represent positive as well as negative numbers. In signed one’s complement representation MSB bit represents Sign bit and rest bits represents data bits. MSB bit 0 represents positive number and MSB bit 1 represent negative number. In signed one’s complement representation Positive numbers are represented as same as unsigned magnitude representation with sign bit and negative numbers are represented as one’s complement of positive number.
Example:
+5=0101 /*same as unsigned magnitude representation with MSB as sign bit. */
-5=1010 /* MSB bit 1 for negative Number, 010 represents ones complement representation of 101(5) or we can say, In signed ones complement representation negative numbers are represented as ones complement of positive number representation of signed ones complement.*/
Problem with signed one’s complement representation: We all know there is nothing like positive zero or negative zero. In this representation there are two different representations for zero.
example:
+0=0000 /*Positive zero representation*/
-0=1111 /*Negative zero representation*/
Practically there should only one representation for zero but there are two representations for zero that why we don’t use signed one’s complement representation in Computer.
Two’s complement of binary number is obtained by adding one to the result of one’s complement of binary number.
Example:
1010=10102
Ones’s complement of 10102=0101
Two’s complement of 10102=0110(0101+1)
Two’s complement representation classified as Unsigned two’s complement and Signed two’s complement.
In all number representation types whether it’s magnitude or complement number representation, positive number always represented as magnitude unsigned binary representation.
Example:
1010=10102
510=01012
Signed two’s complement representation use to represent positive as well as negative numbers. In signed two’s complement representation MSB bit represents Sign bit and rest bits represents data bits. MSB bit 0 represents positive number and MSB bit 1 represent negative number. In signed two’s complement representation Positive are represented as same as unsigned magnitude representation with sign bit . Negative numbers are represented as two’s complement of positive number representation .
Example:
+4=0100 /*MSB sign bit 0 represent positive number and 100 are data bits*/;
-4=1100 /*MSB sign bit 1 represent negative number Two’s complement representation of (+4) */
In signed two’s complement representation , There is only one representation for zero.
+0=0000 /*msb bit 0 represents positive number, 000 are data bits*/
-0=0000 /*1111 is one’s complement of 0000(+0) and adding one to that will give you two’s complement representation of -0 i.e 0000*/
Two’s signed complement representation is used in computer as it has only one representation for zero( +0 or -0)