1
$\begingroup$

I am asking the question even though it has been answered before because I am looking at 3 different pages with 3 different answers right now.

I am wondering how to convert a negative decimal with a fraction, for example

$$-37.125$$

to binary and to hex in two's complement form.

I understand how to do this with a positive number, and I understand how to convert whole numbers into two's complement form.

From what I can tell, the process for the whole number will remain the same (convert to binary, switch zeros with ones, and add one).

However, I have no idea what to do for the fractional negatives because it doesn't seem like you can just treat it as a positive fraction and I tried converting it a couple of times into two's complement but can't get the proper answer by just switching zeros or something similar.

The answer needs to be represented in the form with the Most Significant Bit being a 1 to show it is negative. So the respective conversions would be

$$-37.125 \to 0b11011010.11100000 \to 0xDA.E0$$

Any hints to go to and from binary would suffice, since to and from hex is the same process once you know the binary.

Thank you very much for any help!

$\endgroup$
  • 1
    $\begingroup$ This would depend on exactly how you are defining your binary representation. Computers typically represent fractional numbers with a Floating Point representation that explicitly includes a sign bit: en.wikipedia.org/wiki/Floating_point $\endgroup$ – Paul Sinclair Sep 1 '15 at 4:44
  • $\begingroup$ yes, that is what I meant. Let me clear up my question one second, thank you $\endgroup$ – mrybak834 Sep 1 '15 at 4:46
  • $\begingroup$ I don't see how your edit has addressed Paul's point. The question is phrased as if there were exactly one way of representing fractional numbers in two's complement binary form. That's not the case. To make the question well-defined, you need to first specify the format to which you want to convert the number. Also, it wouldn't hurt if you linked to the three pages with different answers that you mention. $\endgroup$ – joriki Sep 1 '15 at 5:45
1
$\begingroup$

I don't know that this counts as an answer, but it is too much to say in a comment:

Actually, the change does help considerably to understand the question. But the answer is still dependent on the particular implementation. Different systems represent floating point numbers in different ways (even without considering Endianess). Most common (I believe - I am not a computer scientist), is to represent -37.125 exactly the same as 37.125, except for the sign bit $D$ being set. $37.125 = 0b100101.001$. The Exponent $E$ is determined by shifting this until only the leading 1 is to left of the radix $37.125 = 0b1.00101001 \times 2^5$, so $E = 5$. (I'll let you translate it to binary. Note that $E$ is a signed integer and normally written in 2s-complement - the only part of the expression that is.) Since the leading bit is always 1, there is no need to actually store it, so the significand $A = 0b00101001$, followed by however many other 0s you have room for.

However, whether this is the exact form you need it in, I cannot say.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.