Representing negative numbers in binary:
We represent negative numbers using the 2's complement of a number. What this means is that -4 is represented as the 2's complement of 4. So what is the 2's complement of a number. The 2's complement of 0 is 0. For any non zero number, the 2's complement of a number represented by n bits is 2^n - number.
What is -4 in an 8 bit number.
4 is 00000100
-4 is 256 - 4 which is 252 which is 11111100
So -4 can be represented in binary in 2's complement form as 11111100
How do we know if this result is correct. Let us add +4 and -4. This gives us 100000000. If we remove the overflow bit, it gives us 0, which is correct.
If we want to compute the 2's complement manually, we can follow a trick. Take the +ve number and ignore the LSB 0's and the first LSB 1, and then flip everything else.
+4 is 0100
-4 is 1100
To compute the 2's complement mathematically, we should negate all the bits and add 1.
No comments:
Post a Comment