Binary in 2s complement form subtraction/addition help

  • Thread starter Thread starter Slimsta
  • Start date Start date
  • Tags Tags
    Binary Form
AI Thread Summary
The discussion focuses on performing arithmetic operations using binary numbers in signed 2's complement form and verifying the results for overflow. Participants worked through four problems involving subtraction and addition, confirming calculations and identifying instances of overflow. Key points include the conversion of negative binary numbers to their 2's complement forms and the importance of understanding the sign bit in determining valid results. The final consensus is that while some results may appear correct, overflow can still occur, particularly in the context of limited bit representation. The conversation emphasizes clarity in calculations and adherence to the specific requirements of the homework assignment.
Slimsta
Messages
189
Reaction score
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
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.
 
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:
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:
 
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!
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Back
Top