-2

What is the binary representation of 0.00011001100110011001101 − 0.0000000000000000000000000[1100]? Both numbers are represented using fractional binary number representation.

The solution given in my textbook is 0.0000000000000000000000000[1100].

I've tried to do

0.00011001100110011001101 - 0.000[1100]

following the binary subtraction rule (1 - 0 = 1, 0 - 1 = 1, 1 - 1 = 0, 0 - 0 = 0), the answer I got was nothing like the given solution. What is the correct way to do this kind of subtraction?

Note: This problem originates from CSAPP 3rd Practice Problem 2.51.

Charles Z.
  • 11
  • 1
  • 3
  • Where did you learn that 0-1=1? Can you explain the reasoning behind this "rule"? – Scott Hunter Jul 05 '21 at 19:11
  • https://byjus.com/maths/binary-subtraction/ – Charles Z. Jul 05 '21 at 19:14
  • @CharlesZ. "_Service Unavailable in EU region_" – Ted Lyngmo Jul 05 '21 at 19:14
  • 1
    Sorry about that. The website stated that 0 - 1 = 1 because we can borrow from a potential 1 to the left of the current digit so that we can do 2 - 1 = 1. – Charles Z. Jul 05 '21 at 19:16
  • 1
    I afraid you are mixing up different things. Also the "textbook" solution (as you quote it) looks very wrong, the result is most definitely negative. – Eugene Sh. Jul 05 '21 at 19:34
  • Charles Z., "the answer I got was nothing like the given solution" --> what was your result and the given solution? – chux - Reinstate Monica Jul 05 '21 at 20:01
  • @1201ProgramAlarm Sorry for the confusion. But 0.1 could be written as 0.000[1100], that why I wrote that right after the question. – Charles Z. Jul 05 '21 at 20:01
  • 1
    Charles Z. Post is tagged [floating-point]. FP does not have repeated fractions like 0.000[1100]. – chux - Reinstate Monica Jul 05 '21 at 20:04
  • @chux-ReinstateMonica I should have been more clear on the format I used to represent those numbers. In this question, all numbers are represented using fractional binary number representation. In this format, 0.1 could only be represented using a nonterminating sequence 0.000[1100]. I hope that clarifies a bit. – Charles Z. Jul 05 '21 at 20:15
  • 1
    @CharlesZ. If "**all** numbers are represented using fractional binary number representation" is true then 0.1 (base 2) is same as 0.5 (base 10). 0.1 could only be represented using a nonterminating sequence 0.000[1100] contradicts. Still, unclear why the [floating-point] tag as there are no repeating fractions there. – chux - Reinstate Monica Jul 05 '21 at 20:29
  • 1
    If this is the "global" edition of CS:APP, beware that its assembly-language practice problems were broken by incompetent / careless people hired by the publisher. e.g. [CS:APP example uses idivq with two operands?](https://stackoverflow.com/q/57998998). This particular one might or might not be ok - the people who rewrote the problems seem to know some about computer architecture, but have big gaps in their x86-64 specifics, from what I've seen. – Peter Cordes Jul 05 '21 at 21:52

2 Answers2

2

What is the binary representation of 0.00011001100110011001101 − 0.1?

Since the second is larger than the first, reverse the order

-(0.1 - 0.00011001100110011001101)

  0.10000000000000000000000 append zeroes
- 0.00011001100110011001101
---------------------------
  0.01100110011001100110011

Result -0.01100110011001100110011

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

The way I see it, using 1's complement subtraction:

  0.00011001100110011001101 (a)
− 0.10000000000000000000000 (b)

becomes:

     1111111111111111111111  (carry bits)
   0.00011001100110011001101 (copy of a)
 + 1.01111111111111111111111 (1's complement of b)
   =========================
   1.10011001100110011001100 (sum)

Then take the 1's complement of the result, and add a negative sign:

 -0.01100110011001100110011

I don't see any way to get their solution unless they're doing some special kind of truncated subtraction that doesn't allow negative values.

SGeorgiades
  • 1,771
  • 1
  • 11
  • 11
  • Why 1's complement subtraction, not unsigned binary? Because of the repetition out to infinity, instead of being fixed-point? – Peter Cordes Jul 05 '21 at 22:21
  • @PeterCordes, because the page he linked showed the method using 1's complement. – SGeorgiades Jul 06 '21 at 03:49
  • Oh, you mean https://byjus.com/maths/binary-subtraction/, which the OP mentioned in comments, separate from the problem in their textbook. And yeah, *If the result has a carryover, then add that carry over in the least significant bit* so they're just deferring the `+1` in the `~x + 1 = -x` identity, in case that step can be avoided when the final result is negative. – Peter Cordes Jul 06 '21 at 04:28
  • So it's not "1's complement subtraction" signed subtraction, it's *using* 1's complement -> binary addition as a method to implement plain binary subtraction. Aka "**Binary Subtraction** *Using* / *by* 1’s Complement", as that link describes it. As well as naming the process of flipping all the bits, "1's complement" is also the name of a signed-integer representation where that procedure on the bit-pattern negates the value represented. (https://en.wikipedia.org/wiki/Ones%27_complement). 1's complement add/sub is *not* the same binary operation as normal add/sub, hence CPUs doing 2's comp – Peter Cordes Jul 06 '21 at 04:31