Addition and Subtraction using 2's Complement Arithmetic | 2s Complement Addition and Subtraction
Summary
TLDRThis video tutorial from the All About Electronics YouTube channel explains 2's Complement Arithmetic, focusing on how to add and subtract signed binary numbers. It covers various scenarios, including adding two positive numbers, one positive and one negative, and two negative numbers, highlighting potential overflow issues. The video also demonstrates subtraction by converting it to addition of the first number and the 2's complement of the second. Practical examples with 5-bit and 6-bit representations are used to illustrate the process and emphasize the importance of considering the range to avoid invalid results.
Takeaways
- 📚 The video explains 2's Complement Arithmetic, a method for performing addition and subtraction with signed binary numbers.
- 🔢 In 2's Complement addition, you add two numbers as usual, ignoring any carry that extends beyond the most significant bit (MSB).
- 🚫 If a carry occurs beyond the MSB, it indicates a potential overflow condition where the result exceeds the representable range.
- 💡 The script uses 5-bit signed binary numbers as examples to illustrate the process of addition and the concept of overflow.
- 🔄 To find the 2's Complement of a number, first invert the bits to get the 1's Complement, then add 1.
- ➕ When adding two positive numbers, the result should also be positive, indicated by a 0 in the MSB.
- ➖ When subtracting a number, it is equivalent to adding the 2's Complement of the number being subtracted.
- 🔄 For negative numbers, the 2's Complement representation is found by inverting the bits from the left after the first '1' is encountered.
- 📉 Adding two negative numbers should yield a negative result; if the result is positive, it indicates an invalid outcome due to overflow.
- 🛠️ The script demonstrates that performing arithmetic in a higher bit-width (like using 6 bits instead of 5) can prevent overflow and yield valid results.
Q & A
What is 2's Complement Arithmetic?
-2's Complement Arithmetic is a method used to perform addition and subtraction of signed binary numbers. It allows for the representation of negative numbers in binary form and simplifies the process of arithmetic operations.
How is addition performed in 2's Complement form?
-In 2's Complement form, addition is performed by adding two numbers column by column, similar to unsigned binary addition. Any carry generated after the most significant bit (MSB) is ignored.
What is the significance of the MSB in 2's Complement addition?
-The MSB in 2's Complement addition indicates the sign of the result. If the MSB is 0, the result is positive; if it's 1, the result is negative.
What is an overflow in the context of 2's Complement addition?
-An overflow occurs when the result of a 2's Complement addition operation exceeds the maximum or minimum value that can be represented with a given number of bits.
How can you determine if a 2's Complement addition is correct by looking at the sign bits?
-If the sign bits of the two numbers being added are the same and the sign bit of the result is different, the addition is likely incorrect, indicating a possible overflow or underflow.
What is the range of numbers that can be represented in a 5-bit 2's Complement form?
-In a 5-bit 2's Complement form, the range of numbers that can be represented is from -16 to +15.
How do you find the 2's Complement of a binary number?
-To find the 2's Complement of a binary number, first invert all the bits (0s become 1s and 1s become 0s) to get the 1's Complement, then add 1 to the least significant bit.
How is subtraction performed in 2's Complement form?
-Subtraction in 2's Complement form is performed by converting the subtrahend to its 2's Complement and then adding it to the minuend.
What is the result of adding two negative numbers in 2's Complement form?
-When adding two negative numbers in 2's Complement form, the result is also negative, assuming no overflow occurs.
Why is it important to consider the range when performing 2's Complement arithmetic?
-It is important to consider the range in 2's Complement arithmetic to ensure that the result of an operation does not exceed the maximum or minimum values that can be represented with the given number of bits, thus avoiding invalid results or overflow.
Outlines
📐 Introduction to 2's Complement Arithmetic
This paragraph introduces the concept of 2's complement arithmetic, a method used to perform addition and subtraction of signed binary numbers. It explains that addition in 2's complement is similar to unsigned binary addition, where you simply add the numbers and ignore any carry after the most significant bit (MSB). The paragraph also discusses overflow, which occurs when the result of an addition exceeds the representable range of numbers in a given number of bits. An example using 5-bit signed binary numbers is provided, showing how to add two positive numbers, +6 and +7, and how to interpret the result, including the significance of the MSB indicating the sign of the result.
🔍 Understanding Overflow with 2's Complement
The second paragraph delves into the specifics of overflow in 2's complement arithmetic using a 5-bit example. It demonstrates what happens when the sum of two positive numbers, +9 and +7, exceeds the maximum representable positive number in 5 bits, resulting in an invalid result. The explanation includes a step-by-step addition of the two numbers in binary and how the final result's MSB indicates an incorrect sign, signifying an overflow. This section reinforces the importance of being aware of the range limits when performing arithmetic operations in 2's complement form.
🤔 Adding Positive and Negative Numbers in 2's Complement
This paragraph explores the scenario where one number is positive and the other is negative in 2's complement arithmetic. It uses the example of adding +13 and -6, explaining how to find the 2's complement of a negative number and then perform the addition. The summary highlights the process of adding the two numbers, taking into account the carry bits, and interpreting the result based on the MSB. It also contrasts this with the scenario where the negative number is greater in magnitude than the positive number, using the example of adding -15 and +9, and how the result indicates a negative number.
🔄 Adding Two Negative Numbers in 2's Complement
The fourth paragraph focuses on the addition of two negative numbers in 2's complement form. It provides an example with -8 and -4, showing how to represent these numbers in 5-bit 2's complement form and then perform the addition. The explanation includes the step-by-step addition process, the handling of carry bits, and the interpretation of the result based on the MSB. It also discusses the potential for overflow when the result of adding two negative numbers falls outside the representable range, using the example of adding -8 and -9, which exceeds the 5-bit 2's complement range.
➖ Performing Subtraction Using 2's Complement
The final paragraph discusses how to perform subtraction using 2's complement arithmetic. It explains that subtraction can be treated as an addition of the minuend and the 2's complement of the subtrahend. The paragraph provides examples of subtracting both positive and negative numbers, explaining the process of finding the 2's complement and then performing the addition. It emphasizes the importance of considering the range of representable numbers to ensure the result is valid and not an overflow. The summary also mentions that future videos will cover 1's complement arithmetic.
Mindmap
Keywords
💡2's Complement Arithmetic
💡Sign Bit
💡Overflow
💡Range
💡Carry
💡1's Complement
💡Addition
💡Subtraction
💡Binary Numbers
💡MSB
💡Invalid Result
Highlights
Introduction to 2's Complement Arithmetic for signed binary numbers.
Explanation of addition in 2's complement form, ignoring carry after the most significant bit.
Discussion on overflow conditions during 2's complement addition.
Example of adding two positive 5-bit signed binary numbers using 2's complement.
Conversion of 2's complement result to decimal equivalent.
Range of numbers representable in 5-bit 2's complement form.
Illustration of overflow using an example of adding two positive numbers exceeding the range.
Method to identify overflow by comparing sign bits before and after addition.
Case study of adding a positive and a negative number in 2's complement.
Procedure to find the 2's complement of a number to represent its negative.
Addition of a positive and a larger negative number, resulting in a negative outcome.
Technique to find the 2's complement of a result to obtain the positive equivalent.
Addition of two negative numbers and the resulting negative outcome within the 2's complement range.
Overflow example with the addition of two negative numbers exceeding the 5-bit 2's complement range.
Subtraction using 2's complement arithmetic, equivalent to adding the 2's complement of the subtrahend.
Detailed example of subtracting a positive number from another using 2's complement.
Final example demonstrating the importance of considering range during 2's complement arithmetic.
Conclusion emphasizing the importance of range in 2's complement arithmetic and预告 of future videos on 1's complement.
Transcripts
Hey, friends welcome to the YouTube channel ALL ABOUT ELECTRONICS. So in this video,
we will learn about the 2's Complement Arithmetic, and we will see that, how to perform the addition
and the subtraction of the signed binary numbers, using this 2's complement method. So the addition
in the 2's complement form, is very similar to the unsigned binary numbers. So just add the
two numbers, and if there is any carry after the addition, then just ignore it. And in this way,
the result which we are getting is the correct result, except whenever we get the overflow.
So later on we will see that, when this overflow condition can occur. But first of all let us see
the different cases that can occur, while adding the 2 signed numbers in the 2's complement form.
And for understanding let us take 5 - bit signed binary numbers as an example. So
during the addition, the first case is, when the both numbers are positive numbers. Let's say,
we want to add this plus 6 and the plus 7 using the 5 - bits 2's Complement Arithmetic. So first
of all, let us represent this plus 6 and the plus 7 in the 5 - bit form.
So this plus 6 can be represented as 0 0 1 1 0, where this MSB is the sign bit.
Similarly this plus 7 can be represented as 0 0 1 1 1. And as I said, the addition is very similar to
the addition of the two unsigned numbers. That means we will do the column by column addition.
That means here, this 0 plus 1 is equal to 1, while this 1 plus 1 is equal to 0. And there
will be a carry of 1. And if we add this 1 plus 1 plus 1, then the result will be equal to 1,
and the 1 will be the carry. So once again, in this column this 1 plus 0 is equal to 1. And in
the last column, this 0 plus 0 is equal to 0. That means after the addition, the result is
0 1 1 0 1. So as you can see, here since the MSB is equal to 0, that means after the addition,
the result is positive. And if we convert into the decimal equivalent signed number, then it is equal
to plus 13. So in this way, we can perform the addition of the two positive numbers. Now in the
previous video, we have seen that, using the n - bits, the range of number, that can be represented
in this 2's complement form, are from minus 2 to the power n minus 1 to 2 to the power n minus 1
minus 1. So for the 5 - bits, it is in the range of minus 16 to plus 15. So after the addition,
if the number is out of this range, then we will get the invalid result. Or it is the case
of the overflow. So through the example, let us understand the case of the overflow. So let's say,
we want to add the two numbers plus 9 and the plus 7 using the 5 - bits 2's Complement Arithmetic.
So first let us represent this two numbers in a 5 - bit form. So this plus 9 can be represented as 0
1 0 0 1. Similarly this plus 7 can be represented as 0 0 1 1 1. And if we add these two numbers,
then this 1 plus 1 is equal to 0, with 1 as a carry. Similarly, this 1 plus 1 is equal to 0, and
the carry is equal to 1. Likewise this 1 plus 1 is 0, and the carry is equal to 1. Once again this 1
plus 1 is 0, and the 1 is the carry. Therefore, after the addition the number is 1 0 0 0 0.
And after the addition, the result which you are getting is in fact invalid. The reason is that,
the addition of the plus 9 and the plus 7 is equal to plus 16. And as you have just seen,
using the 5 - bits, the maximum number which can be represented in this 2's complement form
is equal to plus 15. Therefore this addition will be always invalid. And even just by looking at the
binary addition also, we can decide that whether this addition is correct or not.
So if we just look at the sign bits of this two number, then it is equal to 0. On the other end,
if you see the sign bit of the result, then it is equal to 1, which indicates that the number is
negative. Now here, we are adding the two positive number, therefore the result has to be
positive. Or in other words, after the addition the MSB of the result should also be equal to 0.
But in this case, the MSB of the result is equal to 1. Which indicates that,
this result is invalid. That means whenever, we are adding the two positive numbers,
whose result is beyond the range of this n-bit 2's Complement representation,
then we will get the invalid result. And it is the condition of the overflow. All right,
so now let us take the second case, where the one number is positive, and the other number
is negative. And first let us consider that, this positive number is greater than negative number.
So let's say, we want to add the plus 13 and the minus 6 using this 2's Complement Arithmetic.
Now this minus 6 is in fact the 2's complement of the number plus 6. So in a 5 - bit form,
this plus 6 is equal to 0 0 1 1 0. So if we just take the 2's complement of this number,
then we will get the minus 6. So in the previous video, we have already seen the different ways for
finding the 2's complement. The one way is first of all find the 1's complement of the number,
and then just add 1 to this 1's complement. So for the 1's complement, just flip all the 0's
with the 1, and 1's with the 0, in the given number. And then if we just add 1 to that 1's
complement, then the result is 1 1 0 1 0, which is the 2's complement of the number
plus 6. Or in other words, this is how the minus 6 can be represented in the 2's complement form.
All right, so now this plus 13 can be represented as 0 1 1 0 1. And as we have seen,
this minus 6 is equal to 1 1 0 1 0. So if we add these two numbers,
then this 1 plus 0 is equal to 1, while similarly, this 0 plus 1 is equal to 1. In the next column,
this 1 plus 0 is equal to 1, while this 1 plus 1 is equal to 0, with 1 as a carry.
So in the last column, this 1 plus 1 is equal to 0, with 1 as a carry. So here we will ignore this
carry. So now if we see the result, then it is equal to 0 0 1 1 1. And here as you can see,
the MSB is equal to 0, which indicates that the number is positive. And if you see the decimal
equivalent, then it is equal to plus 7. So this is how, we can do the addition of the
one positive number and the one negative number. So in this case, the positive number was greater
than negative number. Now let us see the second case, where this negative number is greater
than positive number. So let's say, we want to add this minus 15 and plus 9. So first of all,
let us find the equivalent representation of this minus 15 in the 2's complement form.
So in a 5 - bit form, this plus 15 can be represented as 0 1 1 1 1, and to find the
2's complement, first of all let us take its 1's complement. That means, just by inverting
all the 0's by 1 and 1's by 0, we will get the 1's complement, which is equal to 1 0 0 0 0. And now,
if we just add 1 to it, then the result is 1 0 0 0 1. So this represents the 2's complement of the
number plus 15. Or in other words, it represents the minus 15 in the 2's complement form. So now,
we know that, this minus 15 can be represented as 1 0 0 0 1, while this plus 9 can be represented as
0 1 0 0 1. So now if we just add these two numbers, then this 1 plus 1 is equal to 0, with
1 as a carry. So now this 1 plus 0 is equal to 1, and in the next column this 0 plus 0 is equal to
0. Similarly in the next column, this 0 plus 1 is equal to 1, while this 1 plus 0 is equal to
1. So here as you can see, this MSB is equal to 1, which indicates that the result is negative.
So this is the correct representation in a 2's complement form. So if we just take
the 2's complement of this number, then we will get the corresponding positive number.
Now in the previous video, we have also seen the easy way of finding the 2's complement.
So starting from the LSB, copy all the bits, until we encounter the first one. And then after
replace all the 0's by 1 and 1's by 0. That means if we see the 2's complement of this number,
then it is equal to 0 1 1 0 0 (from right to left) . So until we encounter the first one, copy all the bits
as it is. And after this first one, replace all the 0's by 1, and 1's by 0, in the given number.
That means the 2's complement of the number is equal to 0 0 1 1 0. And its decimal equivalent
is equal to plus 6. Therefore, this number, corresponds to minus 6. And in fact, which is
our expected result. Right? Because the addition of the minus 15, in the plus 9, should be equal
to minus 6. So similarly, now let us see the case when the two numbers are negative. So let's say,
we want to add the two numbers, that is minus 8, and minus 4. So first of all,
let us represent this minus 8 and the minus 4 in the 2's complement form. So we know that,
the plus 8 in the 5 - bit form, is equal to 0 1 0 0 0. So if you find its 2's complement,
then it is equal to 0 0 0 1 1 (From right to left). That means starting from the LSB, copy all the bits,
until we encounter the first one. And then after, replace all the 0's by 1, and 1's by 0, in the
given number. That means the 2's complement of this plus 8, is equal to 1 1 0 0 0.
Similarly this plus 4 in a 5 - bit form, can be represented as 0 0 1 0 0. And its 2's complement
is equal to 0 0 1 1 1 (from right to left) . So in this way, we found the 2's complement representation of this minus 8
and minus 4. So now, let us just add this minus 8 and minus 4. So if you perform the addition,
then this 0 plus 0 is equal to 0, similarly in the next column this 0 plus 0 is equal to 0.
Then this 0 plus 1 is equal to ,1 while this 1 plus 1 is equal to 0, with 1 as a carry. And in
this last column, this 1 plus 1 plus 1 is equal to 1, with 1 as a carry. So here, we will ignore this
carry bit. So after ignoring the carry bit, if you see the result, then it is equal to 1 0 1 0 0. So
here as you can see, the MSB is equal to 1, which indicates that the number after the addition is
negative. And it is in the 2's complement form. So if we take the 2's complement of the number,
then we will get the corresponding positive number. So if we just take the 2's complement,
then its 2's complement is equal to 0 0 1 1 0 (from right to left). And the corresponding decimal number is equal to
plus 12, which indicates that, this number is equal to minus 12 in the 2's complement form.
That means, this number represents the minus 12 in the 2's complement form. Now here after
the addition of the two negative number, the result is still within the 2's complement range.
Bcause for a 5 - bit 2's complement form, we have seen that, the range is from minus 16 to plus 15.
But after the addition, if the result goes beyond this range, then the condition of overflow will
occur. Or we can say that, in that case the result is invalid. So let us take one example,
where the addition of the two negative number goes beyond the 2's complement range.
So let's say, we want to add the two numbers, that is minus 8 and the minus 9, using the 5 - bits 2's
Complement Arithmetic. Now as we have seen, using the 5 - bits we can represent the numbers starting
from minus 16 to plus 15 in a 2's complement form. So if we add the minus 8 and the minus 9 then the
result is equal to minus 17. And as you can see it is beyond the range of 5 - bit 2's complement
form. That means in this case, if we add these two numbers, in a 5 - bit form, then we will get the
invalid result. And the same thing, let us also see that in a 2's complement form. And for that
first of all let us represent this minus 8 and the minus 9 in a 2's complement form. So we know that,
the plus 8 can be represented as 0 1 0 0 0. So its 2's complement is equal to 0 0 0 1 1 (from right to left)
Similarly this plus 9 can be represented as 0 1 0 0 1. So its 2's complement is equal to 1 1
1 0 1 (from right to left). So in this way, we found the 2's complement representation of minus 8
and minus 9. So if you try to add these two numbers, then the result is 1 0 1 1
1. So here, if we just ignore this carry bit, then the result is 0 1 1 1 1. So as I said earlier,
just by checking the MSBs, we can decide whether the addition is correct or not. So here if you
see, then the MSB of the two number, is equal to 1. That means the two numbers are negative.
On the other end, if we see the MSB of the result, then it is equal to 0. Which indicates that,
the result is positive. Now whenever, we are adding the two negative numbers, then the result
should also be negative. Right? But here we are getting the positive result, which indicates that,
this addition is invalid. That means while adding the two numbers in a n - bit 2's complement form,
we also need to be careful about the range. All right, so now let's see,
how to perform the subtraction, using the 2's Complement Arithmetic. So this subtraction in
this 2's complement arithmetic, is very similar to the addition. Let's say, we want to subtract
the 3 from 5, which is equivalent of saying the addition of the plus 5 and the minus 3.
So in the 2's complement arithmetic, it is equal to 5 plus, 2's complement of the number 3.
So we know that, in a 5 bit form, this plus 3 can be represented as 0 0 0 1 1. So its 2's complement
is equal to 1 0 1 1 1 (from right to left). Which indicates minus 3. So now to perform the subtraction, we need to add
this minus 3 and plus 5. So this plus 5 can be represented as 0 0 1 0 1. And as we have seen,
this minus 3 in a 2's complement form, is equal to 1 1 1 0 1. So if we perform the addition,
then this 1 plus 1 is equal to 0, with 1 as carry. So in this next column, this 1 plus 0 is equal to
1. So then after, if we move to the next column, then this 1 plus 1 is 0, with 1 as a carry to
the next column. So once again this 1 plus 1 is 0, with 1 as a carry to the next column. And in
the last column, this 1 plus 1 is 0, with 1 as a carry. So here we will ignore this
carry bit. So after ignoring this carry, if we see the equivalent number, then it is equal to 0 0 0
1 0. And as you can see, here the MSB is equal to 0. Which indicates that after this addition, or in
other words after the subtraction, the resultant is positive. And if we see the decimal equivalent,
then it is equal to plus 2. So in this way, using this 2's complement arithmetic,
we can also perform the subtraction, similar to the addition. So similarly, let's say we want
to subtract this minus 6 from plus 9. Which is equivalent of saying the addition of plus 9 with a
2's complement of the number minus 6. Now we know that, the 2's complement of the negative number is
positive. That means the 2's complement of minus 6 is equal to plus 6. So to perform the subtraction,
we just need to add this plus 9 with plus 6. And if you see the equivalent number, then it is equal
to 0 1 1 1. And the corresponding decimal number is equal to plus 15. So in this way,
using this 2's complement arithmetic it is very easy to perform subtraction,
just like a addition. All right, so now let's take this last example. So let's say,
we want to subtract these two numbers using this 2's complement arithmetic. And here both numbers
are in a 2's complement form. So let's say these two numbers are P and Q respectively.
So in a 2's complement arithmetic, this P minus Q is effectively P plus 2's complement of the number
Q. So first of all, let us find the 2's complement of the number Q. So if we find the 2's complement,
then it is equal to 1 0 0 1 1. And effectively it is equal to minus Q.
So now let's add P to this minus Q. So if you perform the addition, then this 1 plus 1 is 0,
with 1 is a carry to the next column. Then in the next column, this 1 plus 1 plus 1 is equal to 1,
with 1 as a carry to the next column. So now in this third column, this 1 plus 0 is equal to 1.
And in the fourth column, this 0 plus 1 is 1, while in the fifth column this 1 plus 1 is 0,
with 1 as a carry. So if we ignore this carry bit, then the result is 0 1 1 1 0. Now let's see,
whether our addition is correct or not. So if you see the MSB of the 2 number,
then it is equal to 1. Which indicates that, the two numbers are negative. On the other end,
the MSB of the result is equal to 0. So as I said earlier, when we add the two negative numbers,
then the result has to be negative. That means the MSB of the result, should also be equal to
1. But in this case, as you can see, it is equal to 0. Which indicates that, the result is invalid.
So let us understand, why we are getting this invalid result. And for that, let us find the
decimal equivalent of the given numbers. So here, both numbers are in a 2's complement form.
And in the first number the MSB is equal to 1. Which indicates that, the number is negative.
So if we find the 2's complement of the number, then we will get the equivalent positive number.
That is equal to 1 0 1 0 0 (from right to left). Which corresponds to plus 5. That means the given number is equal to
minus 5. Likewise the second number is equal to 0 1 1 0 1. And in decimal, it is equal to plus 13.
So here, what we are doing, we are subtracting this minus 13, from minus 5. And therefore,
the result should be equal to minus 18. But as we have seen in a 5 - bit 2's complement form,
the range of number that can be represented are from minus 16 to plus 15. That means this minus
18 is out of the 2's complement range. And therefore we are getting the invalid result.
But suppose for the same numbers, if we do the 2's complement arithmetic, in a 6 - bit form,
then we will get the correct result. Because in a 6 - bit representation,
the result is within the given range. So let's perform the same arithmetic, in a 6 - bit form.
And first of all, let us represent both numbers in a 6 - bit 2's complement form. So if we try to
represent these numbers, in a 6 - bit form, then we need to copy the MSB of these two numbers in a
additional bit. So this is how, these two numbers can be represented in a 6 - bit form. So here, we
have just copied the MSB of this two number in the additional bit. So by doing so, the number remains
the same. So now, let us find the 2's complement of this second number. That is equal to 1 1
0 0 1 1 (from right to left). So instead of performing the subtraction, now we need to add, these two numbers. And if you
try to perform the addition, then it is equal to 1 1 0 1 1 1 0. So if we just ignore this carry bit,
then the result is 1 0 1 1 1 0. And here as you can see the MSB is equal to 1. Which indicates
that, the number is negative. So if we just take the 2's complement of that number, then we will
get the corresponding positive number. That is equal to 0 1 0 0 1 0 (from right to left), which is equal to plus 18.
That means, this number corresponds to minus 18 in a 2's complement representation. So as you
can see, now in a 6 - bit form, we are getting the correct result. That means, while performing the
addition or the subtraction in this 2's complement form, we should always be careful about the range.
So these are the few things, which we need to keep in mind, during the 2's complement arithmetic.
But I hope through the different examples, you understood how to perform the addition
and the subtraction using this 2's complement arithmetic. So similarly, in the future videos,
we will see that, how to perform the addition using the 1's complement arithmetic. So if you
have any question or suggestion, then do let me know here in the comment section below.
If you like this video, hit the like button and subscribe the channel for more such videos.
浏览更多相关视频
8. CAMBRIDGE IGCSE (0478-0984) 1.1 Signed integers using two's complement
ECAP268 - U01L04 - Fixed point and floating point representation
Adding and Subtracting Integers: A Step-By-Step Review | How to Add and Subtract Integers
Data Representation - Mantissa And Exponents Part 3 - (A Level Computer Science Made Easy (A2) )
How To Convert Binary To Decimal - Computer Science
MTH150 1.2 Properties of Negatives and Technical Communication
5.0 / 5 (0 votes)