I'm confused about where I'm going wrong in the following problem using binary multiplication with two's complement.
I am trying to multiply 12 * -6.
We know that 12 = 01100 and -6 = 11010, and sign-extended we get 00000 01100 * 11111 11010. I tried multiplying these two numbers as follows:
1111111010
x 0000001100
------------
0000000000
0000000000
1111111010
+ 1111111010
---------------
10111110111000
This is definitely not -72, so what am I doing wrong?
Drop the digits from the left that don't fit in the data type:
10111110111000
truncates to
1110111000
You'll find that this is indeed -72.
Related
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.
I've built a four bit adder/subtractor utilizing 4, 1 bit full adders and the input and output are twos complement numbers.
If X=0111 and Y=1000 their sum is obviously 1111.
In decimal this is equivalent to 7 + 8 thus 15 which is what the sum results in.
I am confused however if this result needs to be translated back into "regular" binary by flipping the bits and adding one? So that the answer would be 0001 representing 1 in decimal instead. And that Y in decimal before translation was actually 0110 representing 6 thereby yielding the following in binary 7-6 = 1. If anyone could point me in the right direction I would appreciate it!
It appears you've got the conversion for Y wrong. Y = 10002 = -810.
To represent -6 you take 0110, flip the bits to get 1001 and add one, so Y = 1010. (And 0111 + 1010 = 0001 as you expect.)
To go back, flip the bits of 1010 = 0101 and add one giving 0110 = 6.
Edit to answer your follow-up question:
Let:
X = 0111
Y = 1100
X + Y = 0011 (ignoring overflow)
So whatever we're adding, it equals 3. We know that X = 7.
Y = 1100 => 0011 + 1 = (negative)0100 = -4
7 + (-4) = 3
No translation is necessary, just represent the positive and negative numbers correctly. I think your confusion is coming from the fact that we're "negating" the negative numbers to find the absolute value of that number and sticking a negative sign in front of it, as in the conversion of Y above. That's because negative numbers in 2's complement aren't as readable as positive numbers, but 0100 is still +4 and 1100 is still -4.
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
I am trying to convert FFAD (hex) to a decimal value and then do one and two's complement on it. FFAD is represented as a 16 bit integer.
When I convert FFAD to base 2 I get 1111111110101101.
My problem is how I know if it is a negative number or not?
I have the binary, now to do ones complement normally I would change the last bit from a 0 to a 1 and then flip all the bits, but as a 16 bit integer I don't have any more available bits. Since the 16th bit is a 1 does that mean it is a negative number? How would I do the complement of that? I am just all around confused with this problem and any pointers would be greatly appreciated.
Its negative because it is left padded with ones. Left padding with zeros would indicate a positive number. For example, here are the decimal values for three bit numbers:
011 = 3
010 = 2
001 = 1
000 = 0
111 = -1
110 = -2
101 = -3
100 = -4
Notice, numbers that are left padded with ones are negative and numbers that are left padded with zeros are positive.
Standard procedure for 2's compliment is to negate your bits, and then add 1.
1111111110101101 becomes 0000000001010010. Then you add 1 to get 0000000001010011.
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.