Need help adding with Twos Complement notation - binary

I'm having slight confusion over two's complement. I have reviewed What is “2's Complement”?.
I'm trying to add -2 + -3 = -5. Here's my thought process:
+2 = 0010
-2 = 1110 # in twos complement
+3 = 0011
-3 = 1101 # in twos complement
1101
+1101
-----
10010 # What is this?
I know -5 is 1011 in Two's Complement. But I'm not sure what I did when I added -2 + -3 in the example above.
An explanation of the process to add -2 + -3 would be appreciated. Thank you for your assistance!

In 4 bit 2'scomplement -2 is 1110 and +3 is 0011 so
11110 carry
1110 -2
+0011 +3
----
10001 which is 0001 or simply 1 ignoring the carry in bit 5
Stepping through the process from the right to the left:
1 + 0 results in 1 with no carry
1 + 1 results in 0 with a carry of 1
1 + 0 + the carry of 1 results 0 with a carry of 1
1 + 0 + the carry of 1 results in 0 with a carry of 1
just the carry of 1 results in 1 with nothing more to carry
For reference see the Wikipedia article on 2's complement particularly the section on addition at https://en.wikipedia.org/wiki/Two%27s_complement#Addition. There are a number of online 2's complement calculators to help with conversions and one of them is at http://www.exploringbinary.com/twos-complement-converter/
Let me know if you want to see how -3 + -3 is done, since that is what you attempted. It is a similar process, but be sure start with bit length large enough to avoid overflow as determined when the leftmost two bits in the carry row have different values.

Related

How to add 1 for two's complement when you have a 1 in the first bit? [duplicate]

Using six-bit one's and two's complement representation I am trying to solve the following problem:
12 - 7
Now, i take 12 in binary and 7 in binary first.
12 = 001100 - 6 bit
7 = 000111 - 6 bit
Then, would I then flip the bit for two's complement and add one?
12 = 110011 ones complement
+ 1
-------
001101
7 = 111000 ones complement
+ 1
---------
111001
then, add those two complement together
001101
+111001
-------
1000110 = overflow? discard the last digit? If so I get 5
Now, if I have a number like
-15 + 2
I would then add a sign magnitude on the MSB if it's a zero?
like:
-15 = 001111 6 bit
Would I add a 1 at the end here before I flip the bits?
= 101111
Using two's complement to represent negative values has the benefit that subtraction and addition are the same. In your case, you can think of 12 - 7 as 12 + (-7). Hence you only need to find the two's complement representation of -7 and add it to +12:
12 001100
-7 111001 -- to get this, invert all bits of 7 (000111) and add 1
----------
5 1000101
Then discard the carry (indicates overflow), and you have your result: 000101 which equals to 5 as expected.
For your example of -15 + 2, simply follow the same procedure to get the two's complement representation of -15:
15 001111
110000 -- inverted bits
110001 -- add 1
Now do the addition as usual:
-15 110001
2 000010
-----------
res 110011
To see that res indeed equals -13, you can see that it is negative (MSB set). For the magnitude, convert to positive (invert bits, add 1):
res 110011
001100 -- inverted bits
001101 -- add 1
Hence the magnitude is 13 as expected.
No. The algorithm for two's complement doesn't change based on where the negative value is.

Binary into 2's compliment

if I convert the binary number 000000 into 2's compliment I will get
1's compliment (invert) = 111111
2's compliment (add +1) = here I run into a problem, does this return 000000 and the 1 gets discarded or does this return 1000000?
Thanks in advance!
It discards the 1. The condition is similar to the Arithmetic Overflow.
Strength of two's complement is that it helps us retain a binary representation when signed numbers are to be considered, because in mathematics, the value of 0 is same as that of -0. If we have to give up one entire bit just for sign, then, in a 4-bit word, 0000 would denote 0 and 1000 would denote -0, wasting one representation. The two's complement helps get rid of this. If we assume 4-bit words:
val -val bits of val two's complement bits of -val (1's complement + 1)
0 0 0000 0000 (1111+0001)
1 -1 0001 1111 (1110+0001)
2 -2 0010 1110 (1101+0001)
3 -3 0011 1101 (1100+0001)
...
7 -7 0111 1001 (1000+0001)
8 -8 (no rep) 1000 (0111+0001)
(note that for -8 you have one's complement of 8 in an unsigned manner, i.e.8 = 1000 and hence its one's complement is 0111).
Thus you gain a representation for -8 by making 0 and -0 have the same bit pattern, i.e. 0000. Using this, for n bits, we can represent all integer values between -2^(n-1) to 2^(n-1)-1.

subtracting two's complement for beginner

Guys need some help to solve a question. I am trying to learning Complement notation which is in my bca course. Recently i finished the 2's complement notation chapter, ans stuck at question.
Can some one describe me that how to subtract
a)10 from 14
b)14 from 10
using 2's complement notation.
What i have done is
Above numbers in binary.
10=1010
14=1110
two's complement of the two numbers.
1010=>0101+1=0110
1110=>1110+1=1111
Now i am stuck how will i solve the
a)Subtraction of 10 from 14
b)Subtraction of 14 from 10
Please use descriptive way so i can understand each step for both subtraction. Thanks in advance.
At last, we have found the solution
10 in binary 00001010
2's complement of 10 is 0110
14 in binary 00001110
2's complement of 14 is 0010
4 in binary 0000 0100
2's complement of 4 is 1111 1100
2's Complement Subtraction=>
10 - 14 = (-4) 0000 1010 = +10
+ 1111 0010 = -14
1111 1100 = -4
2's Complement Addition=>
14 - 10 = (+4) 0000 1110 = +14
+ 1111 0110 = -10
10000 0100 = +4
Once you are done with 2' complement,
You just need to add it.
Subtraction of 14 from 10
1010=>0101+1=0110 [This means -10]
14 + (-10)
1110
+
0110
0100 [Do not worry about the carry]
Subtraction of 10 from 14
10=1010 14=1110
2's complement of 1110 is 0010
1 0 1 0 + 0 0 1 0 = 1 1 0 0
2's complement of 1100 is 0100
10 - 14 = -4
1010 - 1110 =0100

Binary Subtraction with 2's Complement

I need help subtracting with binary using 2's representation and using 5 bits for each number:
1) -9 -7 = ? Is there overflow?
-9 = 01001 (2's complement = 10111) and -7 = 00111 (2's complement = 11001)
Now we need to add because we're using 2's complement
10111
+11001
= 100000 But this answer doesn't make sense. Also, I'm assuming there's overflow because there are more than 5 bits in the answer.
2) 6 - 10, same process as before. Negative binary numbers don't make sense to me
1) -9 - 7
-9 - 7 = -9 + -7
9 (binary) = 01001
-9 (2's complement) = 10111
7 (binary) = 00111
-7 (2's complement) = 11001
10111 +
11001 =
110000
This doesn't fit into 5 bits. Removing the overflow we get 10000, which is -16 (binary).
2) 6 - 10
6 - 10 = 6 + -10
6 (binary) = 00110
10 (binary) = 01010
-10 (2's complement) = 10110
00110 +
10110 =
11100
This fits into 5 bits and is -4 (binary).
10111 + 11001 is not 100000 but 110000.
1111
10111
+ 11001
-----
110000
Answer to the 1st question is wrong. To find -9-7 using two's complement, we need follow these steps:
STEP:1 Convertion of first number
1st the binary conversion of 9: 01001
2nd find the complement of binary: 10110
Add 1 to the binary complement: 10110
+1
-----
10111
STEP 2: Convertion of second number
1st the binary conversion of 7: 00111
2nd find the complement of binary: 11000
Add 1 to the binary complement: 11000
+1
-------
11001
STEP 3: Adding
Now add the two outputs -9 + (-7): 10111
+11001
--------
110000
The most important thing is checking the answer whether it is correct or not.
you may use index for the binary digits: 7 6 5 4 3 2 1 0
1 1 0 0 0 0
find the 2 raised to the power of each index having 1 digit.
(-)2^5 + 2^4
*Note (-) is used because in two's complement, the most significant bit (the bit with the highest index) is a sign bit -2^5 + 2^4 = -32 + 16 = -16
which is the correct answer for -9-7=-16. For this reason 2's complement become a popular way of representing negative number. For sign magnitude we need to assume a sign bit, which is hard to implement in a computer, and for 1's complement we need to add 1 to find the correct answer.

Adding and subtracting two's complement

Using six-bit one's and two's complement representation I am trying to solve the following problem:
12 - 7
Now, i take 12 in binary and 7 in binary first.
12 = 001100 - 6 bit
7 = 000111 - 6 bit
Then, would I then flip the bit for two's complement and add one?
12 = 110011 ones complement
+ 1
-------
001101
7 = 111000 ones complement
+ 1
---------
111001
then, add those two complement together
001101
+111001
-------
1000110 = overflow? discard the last digit? If so I get 5
Now, if I have a number like
-15 + 2
I would then add a sign magnitude on the MSB if it's a zero?
like:
-15 = 001111 6 bit
Would I add a 1 at the end here before I flip the bits?
= 101111
Using two's complement to represent negative values has the benefit that subtraction and addition are the same. In your case, you can think of 12 - 7 as 12 + (-7). Hence you only need to find the two's complement representation of -7 and add it to +12:
12 001100
-7 111001 -- to get this, invert all bits of 7 (000111) and add 1
----------
5 1000101
Then discard the carry (indicates overflow), and you have your result: 000101 which equals to 5 as expected.
For your example of -15 + 2, simply follow the same procedure to get the two's complement representation of -15:
15 001111
110000 -- inverted bits
110001 -- add 1
Now do the addition as usual:
-15 110001
2 000010
-----------
res 110011
To see that res indeed equals -13, you can see that it is negative (MSB set). For the magnitude, convert to positive (invert bits, add 1):
res 110011
001100 -- inverted bits
001101 -- add 1
Hence the magnitude is 13 as expected.
No. The algorithm for two's complement doesn't change based on where the negative value is.