2

I've been asked (for homework) to do $ 001001_2 - 110101_2$ (base 2) and to represent the answer in a signed magnitude format.

EDIT: I'm specifically asked:

Perform subtraction on the given unsigned binary numbers using the 2’s complement of the subtrahend. Where the result should be negative, find its 2’s complement and affix a minus sign.

What I did was I took the two's complement of the term being subtracted, so:

110101

(apply one's complement then add one to get the two's complement)

001010
+
000001
------
001011

Then I rewrote the problem as an addition:

001001
+
001011
------
010100

And checked my work using Wolfarm Alpha.

My answer is wrong, and I don't know why. This process worked for the other three problems like it. However I did notice that this should have been a negative number.

I'd like to know what I missed / what led to this process that works on other questions not working in this case.

beep-boop
  • 11,595
  • For the second number $110101$, is this a signed or unsigned number? – Adriano Aug 22 '14 at 18:22
  • Your answer is right if computations are done in 2-complement on 6 bits. @Adriano notice that in 2-complement arithmetic, this is completely irrelevant: you use the same subtraction for signed or unsigned numbers. – Jean-Claude Arbaut Aug 22 '14 at 18:24
  • The question states: "Perform subtraction on the given unsigned binary numbers using the 2’s complement of the subtrahend." – Cody Smith Aug 22 '14 at 18:25
  • @CodySmith With your question restated, your result is actually wrong: you are asked to use a sign if the signed result is negative. Si the correct answer is -101100. – Jean-Claude Arbaut Aug 22 '14 at 18:29
  • 1
    If you use Windows, you can open Calculator and go to View, then "Programmer". It allows you to do base conversions between common bases by typing in your value and switching from one base to the next; it just changes how the value is represented. – kettlecrab Aug 22 '14 at 18:34
  • Both numbers are unsigned at first, so the problem is 9 - 53, the answer should be -44. What I'm asking, is why is my process wrong? According to many people taking the two's complement of the subtrahend and adding should work, as long as you convert negative numbers again via two's complement to get them in a signed magnitude format. – Cody Smith Aug 22 '14 at 18:34

1 Answers1

1

When you are doing arithmetic two's complement, the leftmost bit is $0$ for positive numbers, $1$ for negative numbers. You started with $110101$ (base $2$), which is $53$ (base $10$) when treated as an unsigned number, but you proceeded to treat it as a six-bit number in a two's-complement representation. Since the leftmost bit is $1$, that makes it a negative number; its value then is $-11$ (base $10$). You then proceeded to take $9 - (-11)$, and produced the correct result for that problem, namely the value $20$ (base $10$).

The desired answer, $-44$ (base $10$), is not representable in six-bit two's-complement arithmetic. The range of numbers in that system is only $-32$ to $31$ (base $10$) inclusive.

To correctly treat $110101$ (base $2$) as a positive binary number while doing two's-complement arithmetic, you need to use more than six bits in your numbers, so that the leftmost bit is no longer $1$. Try rewriting your problem using seven-bit numbers, like this:

$$ 0001001 - 0110101 \mbox{(base $2$)}. $$

David K
  • 98,388
  • Thank you very much, that makes since now! Can I assume that any number being treated as unsigned must have a leading zero (far left) in order to have enough range for it's possible result? – Cody Smith Aug 22 '14 at 19:09
  • If you mean what I think you mean, then yes. – David K Aug 22 '14 at 21:39