Frage

I have been through different posts here in Stack Overflow relating my question, but none seem to answer it because in their questions, either the decimal representation is given (mine isn't) or the answer is vague to me (like this).

I'm trying to do subtraction of binary digits in fractions or in floating-point.

  0.0110
- 0.100101

The answer given is -0.001101, but the solution is not shown. Since I haven't seen any direct way (not DEC to BIN) to convert a binary fractional digit to its 2's complement, I tried implementing the solution from this lecture on 2's complement of binary fractions wherein you get the bit by bit complement and add the floating-point part (the background principle of adding the fractional part wasn't explained). Using that, my answer did not match the one indicated.

  1.011010 <- 1's complement of 0.100101
+ 0.011010
__________
  1.110100 <- 2's complement of 0.100101

The 2's complement is then added to 0.0110:

  0.011000
+ 1.110100
__________
 10.001100 <- discard overflow '1'            

I ended up with the erroneous answer of 0.0011. What did I do wrong? Did I forget any principle that I could have used?

War es hilfreich?

Lösung

One goes from one's complement to two's complement by adding one unit. In this case the unit is 0.000001, not 1 (it is 1 for integers, but you are not working with integers but with multiples of 0.000001).

  1.011010 <- 1's complement of 0.100101
+ 0.000001
__________
  1.011011 <- 2's complement of 0.100101

The addition becomes:

  0.011000
+ 1.011011
__________
  1.110011 <- 1.110011 is the two's complement of the absolute value of the answer.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top