Monday, December 21, 2015

Writing negative numbers in binary & Difference between 1's and 2's compliment.

Till now, we've learned to write numbers and alphabets in binary now we'll discuss the methods for writing negative numbers in binary. There are several methods used for writing negative numbers in binary, the most common methods are:

  1.  The left most bit, known as the most significant bit (MSB) as the sign bit and the rest of bits indicate the value. MSB is 0 for positive and 1 for negative.
  2.  one's compliment method, in which we just change the zeroes to ones and ones to zeroes to get the one's compliment of a binary number. For instance, one's compliment of 10101 is 01010.
3. Two's compliment: we simply add 1 to the one's compliment of a number to get its two's compliment. For example, two's compliment of 10101 is:
                                    01010            (one's compliment of 10101)
                                +          1            (adding 1 to it)
                                                
                                    01011             (two's compliment of 10101)

 It totally depends on the machine, whatever method it uses, it works according to that. But most of the machines use 2's compliment method now a days. You'll get to know the reason later in this post. You must be thinking that why do we use 1's and 2's compliment and what is the difference between both of them. Their is a serious complication  in first two methods, where the main difference arrives between 1's and 2's compliment method; the two zeroes problem, we'll discuss this problem but for the time being, for those who don't know much about binary, let me share some basic things with you, n bits compensate 2^n combination i.e. if we have two bits of space we can have the following four combinations at maximum:

00
01
10
11

similarly, three bits will be able to compensate 8 (2^3) combinations. for positive numbers, all of the eight will represent positive numbers, when we take negative numbers into account, the range gets divided in two equal parts between positive and negative numbers. For instance, if we have numbers from zero to seven (eight numbers), when we take negative numbers into account, we get the range -4 to 3 (still eight numbers) i.e. from -2^n-1 to + (2^n-1)- 1 (for standard 2 compliment's system). For signed representation and 1s compliment range will be from -3 to 3, you'd think that these are seven numbers but these are still eight numbers because in signed representation and 1s compliment we have +0 and -0. That's the two zeroes problem.  Lets take three bits and  see step-by-step, how numbers are represented through each method.

        positive  no.                   signed                  1's compliment      2's compliment
        representation           representation
                                                                                                                                           

000            0                               0                                0                      0
001            1                               1                                1                      1
010            2                               2                                2                      2
011            3                               3                                3                      3
100            4                              -0                              -3                     -4
101            5                              -1                              -2                     -3
110            6                              -2                              -1                     -2
111            7                              -3                              -0                     -1


From the table shown above, note  that although representations are eight but the range is from 0-7, obviously that's because of 0. Plus, you need to know that binary representations with their signed representation and 1's  compliment always be in the same range of representation. except in 2's compliment when we get -4 but we don't have a corresponding positive 4.
Also note that in signed representation and 1's compliment there are two representation of 0, +0 and -0, it is not quite right, as we have only one 0 in number line, and it is neither positive nor negative.
This two zeroes problem is solved by the 2's compliment; when we add one to one's compliment.
2's compliment is therefore, the most appropriate method to represent the negatives of binary number. But by just seeing a binary number you cannot determine that whether it is a normal binary number, signed representation or 1's or 2's compliment of a binary number. It is necessary that someone tells you about it. For computer, it knows whether it stored a normal or a 2's compliment of binary number in a specific place.
one more thing, which you need to know is that 2's compliment of a number is not necessarily negative, because 2's compliment of a 2's compliment (negative number) is positive.
But when you are told that a certain number is 2's compliment, it is a convention and an understood thing that it will be negative.

Binary is a whole another world within itself. Although it may seem to be insane to someone, but its is amazing and fascinating, the way you can represent everything including sounds and graphics just by using combinations of 1s and 0s. It is the language of machines and the language of probabilities. The ancient civilizations used this binary in their architecture and astrology.
That's all from the binary. Feel free to contact me in case of any query or confusion.
                                

No comments:

Post a Comment