Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 23rd, 2013, 05:35 AM
 kathy025
Registered User

Join Date: Jul 2013
Location: Manila, Philippines
Posts: 24
Time spent in forums: 7 h 43 m 34 sec
Reputation Power: 0
Converting Decimal to Two's-Complement

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:
Code:
`http://s7.postimg.org/sfyejx8yz/error.png`

I am only using the converter to check my answers. Our professor will not credit work without solution.

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)?

I would love to understand this concept. Thank you.

#2
September 23rd, 2013, 08:39 AM
 Jacques1
You have been warned

Join Date: Jul 2012
Posts: 3,113
Time spent in forums: 2 Months 3 Weeks 4 Days 19 h 40 m 52 sec
Reputation Power: 1063
Hi,

in the two's complement representation, non-negative numbers are always displayed with a leading "0". Otherwise, you couldn't tell the numbers apart. Your "10000" could be either 16 (if interpreted as a simple binary representation) or -16 (if interpreted as a two's complement). So that obviously doesn't work.

+16 simply exceeds the range of possible numbers. 5 digit let us display 2^5 = 32 numbers. This space is evenly divided into 16 non-negative numbers (0 to 15) and 16 negative numbers (-1 to -16). So the range is from -16 to +15. The representations of the non-negative numbers start with a "0". And those of the negative numbers with a "1".
kathy025 agrees: Well explained.

#3
September 23rd, 2013, 12:53 PM
 kathy025
Registered User

Join Date: Jul 2013
Location: Manila, Philippines
Posts: 24
Time spent in forums: 7 h 43 m 34 sec
Reputation Power: 0
Thank you. This is very helpful:
Quote:
 Originally Posted by Jacques1 in the two's complement representation, non-negative numbers are always displayed with a leading "0". Otherwise, you couldn't tell the numbers apart. Your "10000" could be either 16 (if interpreted as a simple binary representation) or -16 (if interpreted as a two's complement). So that obviously doesn't work.

We are required to explain why the conversion is not possible, this is what I wrote (before I read above explanation):
Quote:
 2^5 = 32 The signed range in decimal is -16 to +15, which is 32 digits including 0. Therefore, +16 is out of range.

...which appears to be in the second paragraph. I wasn't sure before, so thanks a lot for confirming.

#4
September 23rd, 2013, 04:27 PM
 Jacques1
You have been warned

Join Date: Jul 2012
Posts: 3,113
Time spent in forums: 2 Months 3 Weeks 4 Days 19 h 40 m 52 sec
Reputation Power: 1063
The wording isn't correct, though. You're talking about "32 digits". But what you mean is 32 numbers.

A digit is a single symbol of a number representation. For example, the binary representation "10111" (which represents the number 23) consists of 5 digits.

#5
September 25th, 2013, 07:38 AM
 kathy025
Registered User

Join Date: Jul 2013
Location: Manila, Philippines
Posts: 24
Time spent in forums: 7 h 43 m 34 sec
Reputation Power: 0
Good catch. Would it be proper if I called it "32 integers" as well?

 Viewing: Dev Shed Forums > Other > Beginner Programming > Converting Decimal to Two's-Complement