0

I am trying to convert FFAD (hex) to a decimal value and then do one and two's complement on it. FFAD is represented as a 16 bit integer. When I convert FFAD to base 2 I get 1111111110101101.

My problem is how I know if it is a negative number or not?

I have the binary, now to do ones complement normally I would change the last bit from a 0 to a 1 and then flip all the bits, but as a 16 bit integer I don't have any more available bits. Since the 16th bit is a 1 does that mean it is a negative number? How would I do the complement of that? I am just all around confused with this problem and any pointers would be greatly appreciated.

Cheesegraterr
  • 517
  • 3
  • 14
  • 26

1 Answers1

3

Its negative because it is left padded with ones. Left padding with zeros would indicate a positive number. For example, here are the decimal values for three bit numbers:

  • 011 = 3
  • 010 = 2
  • 001 = 1
  • 000 = 0
  • 111 = -1
  • 110 = -2
  • 101 = -3
  • 100 = -4

Notice, numbers that are left padded with ones are negative and numbers that are left padded with zeros are positive.

Standard procedure for 2's compliment is to negate your bits, and then add 1.

1111111110101101 becomes 0000000001010010. Then you add 1 to get 0000000001010011.

Mjonir74
  • 261
  • 3
  • 9
  • Thank you, I don't know why I was so confused – Cheesegraterr Oct 04 '11 at 21:57
  • quick question, if I was to do 0AA8... That gives me 0000101010101000 and thats left padded with 0s, so it is postive? And for twos I just keep the same bit pattern? – Cheesegraterr Oct 04 '11 at 22:10
  • Yes, it is positive. To apply 2s compliment, negate all the bits and then add 1. You should end up with 1111010101011000 – Mjonir74 Oct 04 '11 at 22:17
  • I though if it was a positive then you don't flip the bits? Thats what your example says no? – Cheesegraterr Oct 04 '11 at 23:14
  • No. My example shows how to get 2s compliment of your FFAD, which just so happens to be negative. I didn't mean to imply that flipping bits is only valid for negative numbers. The idea behind 2's compliment is to get the negation of your current number in binary. The negation of a negative number would be a positive number, and vice versa. If you don't flip the bits for positive numbers, your 2's compliment would result in a positive number, which would be incorrect because the negation of a positive number is a negative number. – Mjonir74 Oct 05 '11 at 23:21
  • 1
    If you look at my example with the three bit numbers, it will be clear. 001 becomes 110 then you add 1 to get 111 (2s compliment of 1 is -1). If you do the same thing for -1, you will get 1 again. 111 becomes 000 then you add 1 to get 001 (2s compliment of -1 is 1). – Mjonir74 Oct 05 '11 at 23:26