Converting Decimal to Two's-Complement

silver picture silver · Sep 23, 2013 · Viewed 16k times · Source

Instructions: Convert these decimal numbers to 5-bit 2's-complement form, if possible. If not possible, explain why this is so.

(16) base 10

According to the online converter:

From what I understand,

If decimal is positive:

  • STEP 1: Convert magnitude to binary.
  • STEP 2: Pad 0's to desired bitsize.

If decimal is negative:

  • STEP 1: Convert magnitude to binary.
  • STEP 2: Pad 0's to desired bitsize.
  • STEP 3: Invert bits to achieve 1's-complement.
  • STEP 4: Add 1 to achieve 2's-complement.

Since 16 is positive, I simply converted it to binary by repeated division yielding (10000) base 2.

I no longer padded 0's since it is already 5-bits (containing 5 digits).

Can somebody explain to me why the online converter is returning an error (which also I strongly believe is the correct answer)?

Answer

yakiro picture yakiro · Sep 23, 2013

With 5 bits you can represent up to 2^5 different numbers, since you want negative and positive numbers the range is -16 to +15.

so you get error since 16 is out of the range for 5 bits. Change to 6 bits and it should work