Binary subtraction with borrowing

505 Views Asked by At

Consider two number which we are going to subtract: 0x44 - 0x29. How can we subtract using borrowing method. The numbers in () shows the value of the digit in radix-2.

0(127)  1(64)  0(32)  0(16)         0(8)  1(4)  0(2)  0(1)
0(127)  0(64)  1(32)  0(16)         1(8)  0(4)  0(2)  1(1)  -
---------------------------------------------------------------

Starting from right to left we see that 0<1, so we have to borrow from (2) but it is zero. Next we move to (4) and since it is one, we borrow from (4). Therefore:

  1(4)  0(2)  0(1)  =>   
  0(4)  2(2)  0(1)  =>  
  0(4)  1(2)  2(0)

So we can subtract the first three digits

0(127)  1(64)  0(32)  0(16)         0(8)  0(4)  1(2)  2(1)
0(127)  0(64)  1(32)  0(16)         1(8)  0(4)  0(2)  1(1)  -
---------------------------------------------------------------
                                          0     1     1

next, 0<1 and (16) and (32) are zero so we have to borrow from (64)

  1(64)  0(32)  0(16)  0(8)  =>   
  0(64)  2(32)  0(16)  0(8)  =>
  0(64)  1(32)  2(16)  0(8)  =>
  0(64)  1(32)  1(16)  2(8)

So we can continue the subtraction

0(127)  0(64)  1(32)  1(16)         2(8)  0(4)  1(2)  2(1)
0(127)  0(64)  1(32)  0(16)         1(8)  0(4)  0(2)  1(1)  -
---------------------------------------------------------------
0       0      0      1             1     0     1     1

So the result is 0x1B.

Is the procedure correct?

1

There are 1 best solutions below

0
On BEST ANSWER

The correct result is 0x1B. 0x44 - 0x29 = 68 - 41 = 27 = 0x1B. Even without doing the calculation, you can see that 0x29 + 0x20 = 0x49, which is greater than 0x44, so the difference has to be less than 0x20.

The procedure looks fine, the only small thing I see is that the number with just the high bit set (i.e. 1000 0000) is 128, not 127. Otherwise the procedure is just the same as in base 10, except you borrow 2, not 10 (and in base n, you'd borrow n) (or to be more correct, you always borrow 10, except it's "one zero" in the base you're working in).