Either there is some confusion with what is written below or my textbook is wrong.
I was looking at examples in my textbook for addition of 8 bit signed binary digits using 2's complement and came across these examples.
We are required to find if any overflow occurs
Example 1]
Adding +75 and -58
+75 = 01001011 ... (a)
+58 = 00111010
-58 = 11000110 ... (b) [took 2's complement of +58]
now adding (a) and (b) we get
01001011
+ 11000110
1]00010001
It was written in textbook that overflow occurs with an arrow pointing to that extra 1 separated with a square bracket.
This example is ok and from here I understood that if there is and extra 1 then we have a overflow.
(Question 1: Am i correct with what i understood ?)
Example 2: This example confused me
Add 53 and (-13)
53 = 00110101
-(13) = 11110011
now adding both we get
00110101 + 11110011 = 1]00101000
And then they wrote: Carry in and carry out for signed bit is 1. So no overflow. Discarding the carry bit and rest is the positive required result
(The main Question: Even though there was this overflow digit(extra 1) why did they say its not overflow. And what are these carry in and carry out for signed bit written in above sentence.)
I did a lot of Google search but couldn't find a good reasonable solution to this line they talked about, or may be it was in front of my eyes but i didn't understand. Somebody please clarify. Most examples I saw(on stack-overflow and other sites) were for 4 bits that confused a bit more. Somebody please help me clarify, Thanks.
Overflow occurs only when correct answer cannot be interpreted using given number of bits.An extra bit is required to interpret the answer right.Consider the following example of byte numbers addition:
+70 in binary 01000110
+80 in binary 01010000
On addition :10010110
1 in the MSB(most significant bit) indicates that answer is negative which is wrong.However if we include an extra 9th bit as a sign bit(=0) we have the answer as 010010110 = 150.The value of this extra sign bit is equal to the carry as a result of addition of bits in MSB(which in this case = 0).
Reference : Computer System Architecture by M.Morris Mano.
I will try to resolve your doubt with the help of examples, where we will ADD two numbers using signed 2's complement method.
But before that let me tell you the condition when Overflow occur.
Basically, overflow occur when carry into the sign bit is not equal to the carry out of the sign bit.
Example Number 1:
Decimal Carry out Sign bit 2’s Complement
-65 - 1 011 1111
-15 - 1 111 0001
---------------------------------------------
-80 1 1 011 0000
1's complement of 0110000 = 1001111
Adding Overflow digit 1 to 1001111 we will get our answer as 1010000.
With signed bit we will get - 1 1010000 which is equal to -80
Carry into the Sign bit = 1
Carry out of the Sign bit = 1
Therefore, No OVERFLOW
Example Number 2:
Decimal Carry out Sign bit 2’s Complement
-65 - 1 011 1111
-75 - 1 011 0101
--------------------------------------------
-140 1 0 111 0100
Carry into the Sign bit = 0
Carry out of the Sign bit = 1
Therefore, OVERFLOW
I hope, that this must have cleared your doubts about Overflow and signed 2's complement Binary Addition technique.
simplest rule while calculating 8 bit number.
Carry-Bit | Sign-Bit
-----------------------------------------------------
0 | 0 --> No Underflow or Overflow
1 | 1 --> No Underflow or Overflow
0 | 1 --> OVerflow
1 | 0 --> Underflow
Add the following using 8 bit signed 2's complement representation:
25 and -40
75 and 80
Related
Consider the two's complement representation on 3 bits:
3+2 causes overflow.
011+010=100 // why is it 100? I think it should be 101.
And what are the carry in to the most significant bit and carry out from the most significant bit in this case?
the textbook says the carry in to the most significant bit is 1, the carry out from the most significant bit is 0.
I think the text book is wrong. the carry in should be 0 and the carry out should be 1. Am I correct or the textbook is correct?
Please help. Thanks.
Textbook
Right and wrong, I would say, just like so much of life :-)
You're correct in stating that 011 + 010 = 101. The link you posted gave the 100 value of -4 which is a correct interpretation of that value, though not the correct value.
You can generally get the overflowed value by adding the two values without taking overflow into account, then subtracting the range (eight for a three-bit value) if it's more than half the range less one.
In other words, 3 + 2 = 5 but, since this is greater than three (half range less one), you subtract the range 8 to get -3, which is what your correct bit pattern is, 101:
Bit pattern Unsigned Signed
----------- -------- ------
000 0 0
001 1 1
010 2 2
011 3 3
100 4 -4
101 5 -3
110 6 -2
111 7 -1
However, adding the middle bits (with no carry over from the right bits) does actually give you a result of zero and carry of one.
That means the carry into the MSB (left bit) is indeed one. And, when adding those left digits (both zero) with that carry of one, you get one with a carry out of zero.
It may be that you're reading the textbook differently to me but we may have to give it the benefit of the doubt here.
I am trying to add the following two binary numbers together, however I am unable to do so becuase im not sure whether or not this is an overflow?
110101 + 010111
The answer I get is: 1001100
Do I remove the left most 1 in the answer or do I keep it? By removing it I get 12, otherwise the answer is Not right. Am I doing something wrong?
Is this right?
The answer to this question depends upon the size of the word in bits of the system you are talking about. In an 8-bit (or higher) system, and you're doing 2's complement, the sum of
110101 + 010111 = 1001100
Is the same as:
00110101 + 00010111 = 01001100
Is: 53 + 23 = 76 with no overflow or carry out.
If it's a 7-bit system, doing 2's complement, then you have:
0110101 + 0010111 = 1001100
Which is 53 + 23 = -52. There's an overflow, but no carry out.
If it's a 6-bit system, doing 2's complement, then:
110101 + 010111 = (1)001100
Which is -11 + 23 = 12. There's no overflow but there's a carry out. Note that in a 6-bit system you can't have 1001100 technically because it's 7 bits. You would have 001100.
For reference, see The CARRY flag and OVERFLOW flag in binary arithmetic.
I have a question from Patterson's computer Organization book.
Here they gave two numbers A and B which are 8 bit signed integer. The values of A and B are 216 and 255 respectively. They are asking to find A+B and A - B and report if there is any overflow/underflow.
Now my doubt is - you cannot even represent 216 in 8 bit signed numbers. The range is [-128,127]. So the question is invalid!!
I would really appreciate if anyone confirms my doubt.
Thanks.
The question is not invalid at all. In fact, questions of this type are quite common to ensure you understand overflow/underflow just wrap around the bit representations like when an odometer rolls over to all zeros when the milage gets too high. Since I am a Computer Science faculty, as well, I'll try to assist without answering the Patterson problem directly.
You are correct to instinctively feel 216 is not representable in a signed 8 bit number, but let's look at how these things actually work.
Say you have a signed 4 bit machine and need to store the number -11. You might be clever and note that the range of signed 4 bit numbers is [-8,7], so -11 must be impossible. Not so... First, store 11... that's 0000...0000001011_2 in general, but we must squeeze it into 4 bits, losing all but the low order 4 bits. So 11 = 1011_2. At this point you might be saying, "but isn't that a negative number?" Well, if you have any horses, hold them for a minute. Now negate 4 bit 11 to get 4 bit -11... 2s comp negation requires flipping all bits and adding 1, so -1011_2 = 0100_2 + 1 = 0101_2.
Ok, you can stop holding your horses now. It is true that 0101_2 is actually +5, but might it also be just as good as -11? Remember your early math days when your teachers told you to check your results?... we can do the same here to feel all warm and cozy about +5 being the same as -11. For example -11 + 6 = -5, right? So if we add 6 to 0101_2 and get -5, we should be satisfied with our seemingly out-of-range -11, correct? Of course! :-) So let's see... 6 = 0110_2 in 4 bits, now let's add.
0101
+ 0110
------
1011
So what is 1011_2? It's negative; we can tell because the sign bit (left most bit) is 1. To find its magnitude, we can just negate it... -1011_2 = 0100_2 + 1 = 0101_2, which to our astonishment is 5, so the original result, 1011_2 is -5, the answer one should have expected.
Maybe we're not convinced this is Ok. Let's verify -11 is 0101_2 by adding 14. That should give us a result of 3 because -11 + 14 = 3. Uh oh... 14 also seems to be out of range, but let's work like a machine and just do what we're told, no matter how stupid it might seem. +14 is 0000...0001110_2 generally, but we have to squeeze it into 4 bits by losing all but the low order 4 bits. So +14 in signed 4 bits is 1110_2. Let's add that to -11...
0101 // -11
+ 1110 // +14
------
10011 ...but this also needs to get squeezed into 4 bits, so it's 0011
Hey, isn't 0011_2 the same thing as +3? It is! So it looks like you actually can store numbers that appear outside the obvious range and they can still be used accurately for at least some math.
So hopefully this has given you an idea of how you can approach storing 255 and 216 in a signed 8 bit format without worry. But I'll leave the detection of overflow/underflow to you :-P
Good luck.
i want to do the following subtraction using ones complement
Octal(24)-Hex(4B) and give a binary answer
Octal(24) is 20 decimal
and Hex(4B) is 75 in decimal
20->10100
75->1001011
taking 1s complement of 75
0110100 and adding to 20
10100
+0110100
=1001000
adding the carry with the result
001000
+ 1
=001001 which is wrong
Where am i going wrong ?
I am new here, sorry if any mistakes in the way its typed.
You have a small few mistakes in your version. let me show you a correct solution and then show you your mistake(s)
We have the octal number 24 and the hex number 4B. both are fairly easy to translate to binary.
every octal digit represents 3 binary digits.
2 4
+++ +++
010 100
every hexadecimal digit represents 4 digits.
4 B
++++ ++++
0100 1011
now you build the complement:
~01001011
---------
10110100
the you need to add one. Otherwise you get 2 zeros. (+0 => 00000000, -0 => 11111111). this actually makes it a two's complement, but its needed unless you want weird results when crossing the 0-border
10110100
+00000001
---------
10110101
now your complement is done. Next step is to add both numbers
00010100 #The Octal 24
+10110101 #The complement
---------
11001001
The first digit is a 1 therefore its negative (as we'd expect since we did 20 - 75)
Therefore we need to reverse it.
First we subtract one: 11001000
Then we invert it again: 00110111
Which is decimal 55. Therefore 11001001 is decimal -55.
20 - 75 = -55
Voila, we are done :)
First tiny note: you made a small mistake when converting 0x4B (= Hex 4B) into binary format. one digit is wrong :)
Also, you forgot to add one. Then you did some weird stuff i don't get here:
adding the carry with the result 001000 + 1 =001001 which is wrong
Also, you didn't use fixed size numbers which made it impossible to you to find out if the result was negative. I sticked to 8 Bit here (except during octal -> binary conversion). (Keep in mind that with 8 bit your number range is from -127 to +128.) And in the end - as you couln't see its a negative number - you did not revert the process.
I hope this explanation helped you out :)
This is a question which I saw in a past paper. I think I understand 10s complement, but don't understand the following question about two's compliment. Also, what does two's compliment have to do with binary?
What number in base 10 is represented by 1110 in 2s complements with k=4? Would the answer change if k=5?
Please Explain the answer, thanks for any help!
Two's complement is a method to represent negaitve numbers in binary. I don't understand how it works, but it does, there is mathematical foundation behind it.
take for example the number 6. In binary it's 0110. Then to represent -6 you need to apply two's complement algorithm on it.
The algorithm is to copy all the digits from right to left. The first time you encounter 1 you leave it as it is, but from that point on you invert all the digits as you advance left. In this example let's go right to left: First we have 0. Then we have 1. We copy it, so meanwhile we have 10. Since we encountered 1 we now need to invert all the bits. So the next one is 1, we copy it as 0, so we now have 010. The leftmost bit is 0 so we invert it to 1 and so we end up with 1010. This is -6 in 4 digit. Negative numbers in two's complement always have 1 for MSB (the leftmost bit - Most Significant Bit).
Before I continue, there is a short way for the conversion. you simply invert all the bits then add 1, and get the same result. If we invert 0110 we get 1001. Adding 1 gives, again 1010. Don't know how it works but it does.
How much is 8 - 6? It's like 8 + (-6). In two's complement it's 1000 + 1010 = 10010
Since we work with 4 digits, we trim the MSB and get 0010 which is 2, which is 8 - 6. It works.
If you take -6 (1010) and apply the two's complement on it again, you get (using the second method) invert(1010) + 1 = 0101 + 1 = 0110 = 6. So applying two's complement algorithm to negative numbers reveals their absolute value.
And now we can move on to the second part of your question: 1110. In the positive numbers world this is 14. But in the workd of both positive and negative numbers, we see that since the MSB (most signigicant bit) in this number is 1, the number is negative. Like in the -6 example, applying to it two's complement will give it's absolute value. Hence: Invert(1110) + 1 = 0001 + 1 = 0010 = 2. So 1110 is -2.
I beleive that k in the question is number of digits. If k is 5 then how do we represent -2?
To answer that we start with 2 represented in 5 digits, and then apply the two's complement conversion.
2 is 00010. Two's complent on it is invert(00010) + 1 = 11101 + 1 = 11110.
You can conclude that if we had 8 bits then -2 would be 11111110.
This phenomenon is called sign extension. It says that if a negative number can be represented in k bits, then: a) the MSB will be 1, and b) if you want to use more than k bits, then all the bits from the original MSB and left will all be 1.
And again, there is mathematics behind it to prove it, which I'm not familiar with.
By the way, you can look in my website. It happened that your question hits exactly the problem I solve in a product called ChordBits, where you can turn bits on and off, and among other options, you can apply two's complement on them and view what it looks like (the shareware version is fully functional). www.codechords.com
Cheers