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.
Related
Question on an assignment:
How to express -20 (in decimal) as fixed point notation, 8 bits, 2's complement.?
Answer:
EC(16 ---> in hexadecimal.
Question:
I understand how to get that answer. I convert the -20 in base 10 to 2's complement and you get 11101100. 1110=E in hexadecimal and 1100=C in hexadecimal.
What is confusing me, however, is that EC(16 is equal to 236 in decimal. How can EC(16 equal both -20 and 236 in decimal??!
I am confused as to how that works?
As Juan wrote: It just depends on how the bits are interpreted. More specifically, in your case it depends only on how the most significant bit is interpreted. The lower seven bits in both cases add up to 22 + 23 + 25 + 26 = 4 + 8 + 32 + 64 = 108. Now, the highest bit if used as an ordinary value bit has the value 27 = 128 and thus makes 108 + 128 = 236, while if used as the sign bit it has the value −27 = −128 and thus makes 108 − 128 = −20.
So basically I am doing this thing and it says what is 00100110 + 01010111.
00100110 = k in binary
01010111 = X in binary
I naturally went to wikipedia and searched Alphabet. This gave me a grid where it showed me what letter corresponded to what number.
k is 11
X is 24
I added these together to get 35. I put in 35,1124,2411,13 and all of those in binary. These were ALL incorrect somehow.
I do not know what else it could be!
Characters of the alphabet are often used as a way of tagging a number so it can be easily referred to, so if the problem is not specifically asking you to guess a character out of it, the answer would be 125 in binary.
100110 = 38
1010111 = 87
100110 + 1010111 = 1111101 = 125
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
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.
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