September 23rd, 2013, 04:35 AM
-
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.
September 23rd, 2013, 07:39 AM
-
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".
Comments on this post
September 23rd, 2013, 11:53 AM
-
Thank you. This is very helpful:
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):
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.
September 23rd, 2013, 03:27 PM
-
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.
September 25th, 2013, 06:38 AM
-
Good catch. Would it be proper if I called it "32 integers" as well?