How am I supposed to perform this subtraction?

For instance: $01110000_2−11101011_2$

$01110000_2$ has a positive sign, so there is no need to perform two's complement. $11101011_2$ has a negative sign, so I need to perform two's complement.

Two's complement for $11101011_2$ is $00010101_2$

Finally, I must perform the subtraction $01110000_2 - 00010101_2 = 1011011_2$

  • 1
    $\begingroup$ We discourage "please check whether my answer is correct" questions, as only "yes/no" answers are possible, which won't help you or future visitors. See here and here. Can you edit your post to ask about a specific conceptual issue you're uncertain about? As a rule of thumb, a good conceptual question should be useful even to someone who isn't looking at the problem you happen to be working on. If you just need someone to check your work, you might seek out a friend, classmate, or teacher. $\endgroup$ Apr 21 '17 at 18:54
  • $\begingroup$ This is already covered in standard resources, such as textbooks, or in Wikipedia: en.wikipedia.org/wiki/Two%27s_complement, en.wikipedia.org/wiki/Two%27s_complement#Subtraction. $\endgroup$
    – D.W.
    Apr 21 '17 at 19:57

Two's complement is a way of representing positive and negative numbers in binary. There is a very good explanation here: https://stackoverflow.com/questions/1049722/what-is-2s-complement

Basically, the really clever part about two's complement is that arithmetic works without making any special changes. Let's look at that in action with n addition operation: $10000001 + 00010000 = 10010001$. Notice that no special actions were taken in finding the sum. Nevertheless, what I've written there is that $-127 + 16 = -111$.

Two's complement is really just a clever way of encoding the numbers designed to make arithmetic straightforward.

By the way, an easy way of reading two's complement numbers is just to reverse the sign on the highest bit. To illustrate what I mean by this, regularly encoded (non-complement) 8-bit integers like $10010011$ and $01000010$ could be found by adding $1 + 2 + 16 + 128 = 147$, and $2 + 64 = 66$ respectively.

If we were to designate these same numbers as two's-complement, we would just reverse the sign on the 128s bit. Now our two numbers would be $1 + 2 + 16 -128 = -109$, and $2 + 64 = 66$. The second number didn't change because the highest bit was a $0$ already, so it doesn't matter if we are adding or subtracting it.

So, if you were to try to subtract $01110000−11101011$, let's get an answer first so that we can check ourselves. $64+32+16 = 112$, and $-128+64+32+8+2+1 = -21$. You are performing the subtraction $112 - -21$. This means that our final answer should be $133$.

So, just as you can subtract by negating before you add (ie. $112 + 21$ instead of $112 - -21$), we can perform a similar operation here. If we negate the second number, we flip the bits and add one: $00010100 + 1 = 00010101$. But now that we've negated the number, our subtraction has become an addition problem. So, our original $01110000−11101011$ has become $01110000 + 00010101$

+ 00010101

$10000101$ is $-128 + 4 + 1$, which equals...


-123?? Wait, what happened?

We've fallen into the classic two's complement trap! Because we only have 8 bits, we only have a limited range of possible numbers. 8 bits allows for 256 possible values. Two's complement divides these values nicely: half are positive and half are negative, giving us a range of $-128$ to $127$. Why one less in the positives? It's because Zero has to be somewhere, and since $00000000$ doesn't start with a leading $1$, it comes out of the positive space.

If you're just doing pure math, simply add an extra digit, and you'll be out of trouble. $010000101$ is $128 + 4 + 1$, which equals $133$. If you're on a computer, however, you have to pay more attention to this problem.

When you go outside of the range of your bits, you are said to have an overflow error. Modern processors and languages have evolved ways of dealing with this problem, typically through flagging operations that might have caused overflow.

  • $\begingroup$ Thanks. So my operation $01110000+00010101=01110101$ is correct $\endgroup$
    – User1911
    Apr 21 '17 at 13:28
  • $\begingroup$ No, because the 16s digits of both numbers are 1, and 1 + 1 = 0 (carry the 1). Also, if I answered your question, don't forget to accept the answer. Because, what is the purpose of life if it isn't Internet Points? ;-) $\endgroup$
    – Ben I.
    Apr 21 '17 at 13:31
  • $\begingroup$ Definitely, will do once i have got the understand correctly. $\endgroup$
    – User1911
    Apr 21 '17 at 13:32
  • $\begingroup$ Thanks, now it makes bit sense. Can you help me check if the explanation i have written it correct in the question description. $\endgroup$
    – User1911
    Apr 21 '17 at 13:33
  • $\begingroup$ I think I understand your question better. Please see my revised answer. $\endgroup$
    – Ben I.
    Apr 21 '17 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.