Binary in 2s complement form subtraction/addition help

In summary, the following binary in signed 2s complement form. The arithmetic operations performed and verified the answers. The results yielded an overflow for each computation.
  • #1
Slimsta
190
0

Homework Statement


The following binary in signed 2s complement form. Perform the indicated arithmetic operations and verify the answers. Indicate whether overflow occurs for each computation.
a) 110001 – 010010
b) 001011 + 100110
c) 101110 – 110111
d) 100111 + 111001

Homework Equations



The Attempt at a Solution


a) 49-18=31 and
110001 – 010010 = 011111 (which =31)

b) 11+38=49 and
001011 + 100110 = 110000 (which =-48)
--> overflow

c) 46-59=-13 and
101110 – 110111 = 110111
-->2s comp --> = 001001 (which =-13)

d) 39+57=96 and
100111 + 111001 = 100000 (which =32)
--> overflowi hope my answers are right.. can you find any mistakes?
im just a little bit confused as to when to do the 2s comp once i get the answer (like in part C), should i try do the 2s complement just to try get the correct answer and avoid an overflow?
 
Physics news on Phys.org
  • #2
Slimsta said:

The Attempt at a Solution


a) 49-18=31 and
110001 – 010010 = 011111 (which =31)
Ok, I'll go through this one step-by-step. Hopefully you'll get how to do the other ones.

Firstly, being 6 digit binary numbers, I'll number the digits from 0 to 5, with 0 being the least significant bit. So bit 5 is a sign bit. A number with a 1 in bit 5 is negative, and is a 2's complement of the number which is negative. For example:

110001 is negative. Let's find what number it is. Take it's 1's complement:
001110 Now add 1 to it:
001111 Which is equal to 15. Thus that number is actually -15

We don't want to actually use a subtraction here. That's part of the point of 2's complement. So we take out the subtraction by making the second operand negative and turning the operation into addition:
010010 Take 1's complement:
101101 Add 1:
101110 And that's the number in 2's complement form. We now do the operation as an addition:

110001+101110 = 011111
HOWEVER, we also overflowed. You know how to add binary numbers on paper, right? Super easy. The answer would come out to 31, yes, but it's wrong. Checking it out, we can convert them to non 2's complemented numbers. We get:

-15 - 18 = -33

Note how, since bit 5 is the sign bit which would otherwise be worth 32 that we can't really represent -33 in that small a space. The bit worth 32 is otherwise occupied. In fact, if you add two positive numbers that end up putting a 1 into bit 5, you would end up thinking that it's a negative number, but it's not. It would be an overflow as well.

That should be plenty to get you going on the next few. Of course, let me know if you have questions or didn't quite get it.
 
  • #3
ok that makes sense.
so for part B, it would be 11+(-26) = -15
and i'd just leave the addition as 001011 + 100110 = 110001
take the 2's comp of 110001 since i have to return it to the original 2s signed # it would be 001111 which is 15 but it's negative so -15
no overflow here.

then part C, -18 - (-9) = -9
101110 – 110111 ==> 2s comp ==> 101110 + 001001 = 110111 (or -9)
no overflow here.

and part D, -25 + (-7) = -32
100111 + 111001 = 100000 (which =-32)
again, no overflow here.

is that correct?
 
Last edited:
  • #4
You got it! Even the tricky one in D (mostly - see below). The largest negative number is kind of a special case. You can see that you can represent numbers from -32 to +31 with those 6 bits.

You might want to go through the wiki on 2's complement and read the addition section if you're not clear when a problem has occurred that would give an invalid result:

http://en.wikipedia.org/wiki/Two's_complement

If you carry out of the most significant bit (MSB) but not into it (or vice-versa), then you have a problem. But not all overflows (where a 1 is supposed to be carried but you're out of bits) lead to invalid values. So you might not be 100% correct on the overflows. I'd say that D does overflow, but the result is valid.

That last one does overflow, but it's not an error and the value is correct since it carries into AND out of the MSB. Anyways, the section on addition probably describes it better than I do (including a "trick" to quickly figure out if the result is invalid by looking at the carry bits).

But looks like you've got it. Good stuff. :approve:
 
  • #5
sweet! It made sense to me when you explained it and i was like "hmm well that's easy stuff lol" and it took me like 5 minutes to fix my mistakes haha
i don't think my teach wants us to go into that deep level of explanations. if i get the same number for the result in binary and in normal digits and don't write "overflow", then she is happy with that.

Thanks a lot for your time and help!
 

1. What is binary in 2s complement form?

Binary in 2s complement form is a method of representing negative numbers in binary form. It is based on the idea of inverting all the bits and adding 1 to the result. This allows for a more efficient way of performing subtraction and addition operations.

2. How is subtraction performed in binary 2s complement form?

In binary 2s complement form, subtraction is performed by adding the 2s complement of the subtrahend (number being subtracted) to the minuend (number being subtracted from). This is equivalent to changing the sign of the subtrahend and then adding the two numbers together.

3. How is addition performed in binary 2s complement form?

Addition in binary 2s complement form is performed in the same way as regular binary addition. The only difference is that if the result of adding two numbers together exceeds the maximum value that can be represented, the most significant bit (MSB) is dropped and the result is taken as the next lower value.

4. Why is binary 2s complement form used?

Binary 2s complement form is used because it allows for a more efficient way of representing and performing operations on negative numbers. It also eliminates the need for a separate negative sign, making calculations easier to perform.

5. What is the range of values that can be represented in binary 2s complement form?

The range of values that can be represented in binary 2s complement form depends on the number of bits used. For example, with 8 bits, the range is -128 to 127. With 16 bits, the range is -32,768 to 32,767. The range increases as the number of bits increases.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
13
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
19K
Back
Top