Adding and subtracting two's complement - binary

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.

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.

Need help adding with Twos Complement notation

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.

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.

Subtracting binary with numbers of different lengths

I am trying to solved: 1111 – 10010 (binary)
I would like to use two's compliment to solve it. I realize that the answer will be negative, but I don't know how to get it. I tried putting a zero before the first number (01111) to give the numbers equal number of 1s and 0s. Also, how will I know the answer is negative?
01101
+ 00001
____________
01110 <-- two's compliment
01110
+01111
________
11101 //this isn't right
I think the easiest way to solve this problem is to beak it into small steps.
My first assumption is that you are trying to solve 15 (1111 (binary)) - 18 (10010 (binary))
I find the easiest way to do subtraction in two's complement is by the method of complements which is instead of trying to subtract the positive 18 from the positive 15 ( +15 - (+18)), we instead add negative 18 to positive 15 ( +15 + (-18) ). This has the same result but is easier to do in two's compliment ( note: you can't do this if your number system does not have negative numbers)
So we have to take the number 15 and -18 and convert them into two's complement numbers. Since 18 is represented in binary with 5 bits we need to use at least 6 six bit to represent -18 in twos complement.
To convert -18 into two's complement we take 18 in two's complement 010010 flip those bits (turn the 0s into 1s and 1s into 0s) 101101, and then to add one to the flipped bits using binary addition
1 (carried digits)
101101 (-19 (flipped 18 ))
+ 000001 (1)
_________
101110 (-18)
To convert 15 into two's complement we take 15 in binary (1111) and then we add zeros on the left until it has the same amount of digits as -18 (101110) 001111
Now that we both numbers in two's complement we can add them together using binary addition
111 (carried digits)
001111 (15)
+ 101110 (-18)
_________
111101 (-3)
This give us -3 in two's complement which is the correct answer (15 - 18 = -3).
You can learn about two's complement looking at the twos complement wiki page

Decimal to binary number conversion

What is the binary form of -10? How it is calculated?
To convert -10 (decimal) to binary:
Repeatedly divide the absolute value (|-10| = 10) of the number by 2 until you get 0 in the quotient:
(10 / 2 = 5 R 0)
(5 / 2 = 2 R 1)
(2 / 2 = 1 R 0)
(1 / 2 = 0 R 1) // zero is the value in the quotient so we stop dividing
Place the remainders in order to obtain the binary equivalent:
1010
For an 8-bit cell the answer is 0000 1010, 16-bit cell 0000 0000 0000 1010, and so on.
Take the one's complement by inverting the bits (we will assume an 8-bit cell holds the final value):
0000 1010
1111 0101 // bits are inverted
Now take the 2's complement by adding 1:
1111 0101
+ 1
----------
1111 0110 // final answer
What happens with a 4-bit cell?
The one's complement would be:
1010
0101 // inverted bits
Taking the 2's complement produces:
0101
+ 1
----
0110 // final answer for a 4-bit cell
Since the number should be negative and the result does not indicate that (the number begins with 0 when it should begin with a 1) an overflow condition would occur.
Take the binary form of 10, invert all the bits, and add one.
10 0000 1010
invert 1111 0101
add 1 1111 0110
Follow this link. You can find the binary form of a negative number say -n by finding out the two's complement of n.
Your question has no "right" answer. First, you have to define the representation you want to use. Do you want to use two's complement, ones' complement, sign-magnitude, or something else? Then you have to define how many bits to use.
Let's say we are working with 5-bits wide representations.
+10: 0 1 0 1 0
Let's look at sign-magnitude system. In this system, the most significant bit (bit 5) is 1 if a number is negative, and 0 otherwise. The rest of the bits represent the magnitude (absolute value) of the number.
So we get:
-10: 1 1 0 1 0 (sign-magnitude, 5 bits)
Let's look at ones' complement now. Here, a negative number is represented by just changing 1s to 0s and vice-versa (hence the name ones' complement—you complement a number with respect to a long sequence of 1s).
So we get:
-10: 1 0 1 0 1 (ones' complement, 5 bits)
Finally, let's look at two's complement system. In this, we take a number in its ones' complement system, and then add 1 to it.
So we get:
-10: 1 0 1 0 1
1
---------
1 0 1 1 0 (two's complement, 5 bits)
---------
Thus, the binary representation of a negative number depends upon the system we use, and the number of bits available to us.
Also, you might have noticed the position of the apostrophe in ones' complement and two's complement. Why is is not one's complement or twos' complement? Then answer, from Knuth:
A two's complement number is complemented with respect to a single power of 2, while a ones' complement number is complemented with respect to a long sequence of 1s. Indeed, there is also a "twos' complement notation," which has radix 3 and complementation with respect to (2...22)3