A simple question to ask anyone who is working or has studied in computer sciences but only few would be able to answer this. The whole idea of Binary representation is pretty simple and we all know how to represent unsigned bits.
In unsigned bit notation all the bits are part of a number. The number is always positive and a simple 8 bit number ranges from 0 (00000000) to 256(11111111).
Now to accomodate a negative number we have to some changes on how the Most Significant Bit or MSB (the one to the extreme left) is treated. If we consider setting MSB as 0 for positive numbers and 1 for negative numbers then 1 is 00000001 and -1 is 10000001.
Now this is where the requirement for two's complement came up.
If we go and add the numbers 1 and -1 in a traditional way we get
0000 0001
+ 1000 0001
------------------
(1) 0000 0010
-------------------
The resulting number is (+)2 which is something we never expected. The need for Two's complement was to actually get 0 when you add -1 and 1. Getting two's complement of a negative binary number is pretty easy.
One simple way is to invert all the digits if a given binary number and add 1 to it.
So for 1 ( 0000 0001), after inverting all digits it becomes 1111 1110. This after adding 1 becomes 1111 1111.
Lets add 1 and -1 in two's complement.
0000 0001
+ 1111 1111
-------------------
(1) 0000 0000
-------------------
The result is 0 which is what's expected.
No comments:
Post a Comment