2

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?

Community
  • 1
  • 1
ellekaie
  • 315
  • 6
  • 19
  • When you write “decimal representation”, do you mean “fractional representation”? – Pascal Cuoq Jan 26 '14 at 18:30
  • No, I really mean the decimal counterpart of the binary number. Just like in this [question](http://stackoverflow.com/questions/15463491/how-to-represent-a-negative-number-with-a-fraction-in-2s-complement?newreg=3ed1f9f18894465881b1b25a5de31573). – ellekaie Jan 26 '14 at 18:32
  • But in my case, there is no decimal involved. Pure signed binary numbers to be subtracted. – ellekaie Jan 26 '14 at 18:33

1 Answers1

3

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.
Pascal Cuoq
  • 75,447
  • 6
  • 144
  • 260
  • Getting the two's complement of `1.110011` to get its negative value: Adding `0.001100` (1's comp of @PascualCuoq's answer) to `0.000001` is INDEED `-0.001101`! The `0.000001` part is not taught to us in our lecture, so thank you very much! – ellekaie Jan 27 '14 at 01:12
  • @PascualCuoq, I have an additional question! What if there are high values at the left of the fixed point? For example, `1011.01101`. After getting its bit by bit inversion, am I right if I say that I should add `0001.00001` to that to convert it to its 2's complement? – ellekaie Jan 27 '14 at 16:09
  • 1
    @ellekaie No, still `0000.00001`. You can see it as adding integers represented in 2's complement `n` and `m`, where `n` and `m` are the numbers of 1/32ths in the original numbers. If you see it this way, it is natural to add `0000.00001` to convert one's complement into two's complement. – Pascal Cuoq Jan 27 '14 at 16:22
  • Thanks for this answer.. But just would like to clarify.. is it `0.00001` or `0.000001`? Your answer has five zeros but on your comment it's four.. Does it depend on the length of the decimal fraction? @PascalCuoq. Thanks – Severino Lorilla Jr. Apr 04 '16 at 00:20
  • 1
    @severinolorillajr my answer replies to the question and my comment replies to a comment. They are both correct as far as I can tell. – Pascal Cuoq Apr 04 '16 at 20:53
  • i see.. so does it depend on the length of the decimal fraction? I assume that for any number with fractions we will add .xxx1 instead of 1, depending on the number of fraction digits? @PascalCuoq – Severino Lorilla Jr. Apr 05 '16 at 00:53