donysukardi Admin

Number of posts: 54 Age: 19 Location: Holland Drive Registration date: 2008-10-05
 | Subject: Converting Negative Decimal Number to Binary October 10th 2008, 23:44 | |
| Signed numbers
The following section is optional. Most of the time when we deal with binary numbers and bit operations, we use unsigned numbers. However, it is interesting to examine how signed numbers are dealt with.
Signed numbers are typically stored using a method known as two’s complement. In two’s complement, the leftmost (most significant) bit is used as the sign bit. A 0 bit means the number is positive, and a 1 bit means the number is negative. Positive signed numbers are stored just like positive unsigned numbers. Negative signed numbers are stored as the inverse of the positive number, plus 1.
For example, here’s how we convert -5 to binary:
First we figure out the binary representation for 5: 0000 0101 Then we invert all of the bits: 1111 1010 Then we add 1: 1111 1011
Converting -76 to binary:
Positive 76 in binary: 0100 1100 Invert all the bits: 1011 0011 Add 1: 1011 0100
Why do we add 1? Consider the number 0. If a negative value was simply represented as the inverse of the positive number, 0 would have two representations: 0000 0000 (positive zero) and 1111 1111 (negative zero). By adding 1, 1111 1111 intentionally overflows and becomes 0000 0000. This prevents 0 from having two representations, and simplifies some of the internal logic needed to deal with negative numbers.So, basically if you want to find the binary number of, for example, -100, what you have to do is get the binary number of 100, inverse the number (0 to 1, 1 to 0) and add 1. That's it! |
|
citsoon
Number of posts: 41 Age: 22 Location: Woodlands Registration date: 2008-10-07
 | Subject: Re: Converting Negative Decimal Number to Binary October 10th 2008, 23:55 | |
| yaya, i discussed with dony for half an hour, thank him for getting this explanation and set my mind clearer in this conversion. |
|