We assume that 4bit 2's complement system is used to represent both positive and negative numbers
Suppose we have to find 2's complement of no. 3
We can subtract this no. with 2^4
that is ,
= 2^4 - 3
= 13
=1101 (which represent -3 , in 2's complement system)
//* there is another way of finding 2's complement , taking 1's complemen of the
number and add 1 to it.
In the book rule is given to subtract two no.
Rule : to subtract two no X and Y , that is X - Y , form 2's complement of no. Y and add it to X.
Suppose we have to subtract two no. (-7) and (-5), then according to rule, we need to find 2's complement of the no (-5), and then add it to (-7).
Book solution :
I need to know , how 2's complement of -5 is 0101.
To find the 2's complement of a number you:
invert the bits
add 1
Example (2s complement of 5):
5 = 0101 in binary
invert the bits:
1010
Add 1:
1011 <- 2s complement of 5 is 1011
Example (2s complement of -5):
-5 = 1011 in binary
invert the bits:
0100
Add 1:
0101 <- 2s complement of -5 is 0101
Related
Last week I learned about arithmetic with binary numbers especially substraction with two's complement, it was pretty easy so far but something bothers me a little bit. Why is 0 - 1 = 1 with a borrow of 1?
Sure its -1 but should we get some result like 1001 (4 bits for size)? Can someone please explain?
0-1 is not 1 in binary
0-1 does not equal 1 in two's complement with signed numbers. But its representation does have alot of ones in it: you should get 1111.
Negative numbers in two's complement:
In two complement's format (which computers use) with signed magnitude, one bit is reserved to represent the sign of the number. By convention this is the leftmost bit, where 0 indicates a positive value and 1 indicates a negative value.
To get the representation of a negative number we follow two steps:
Flip all the bits
Then increment by one
Additive Inverse of 7:
7=0111
1000 // Flip the bits
-7=1001 // Add +1
So why do we get 1111 when we subtract 0-1?
How do we substract in two's complement? We subtract A-B by taking the additive inverse (the opposite) of B and adding the numbers.
So with A=0 and B=1:
Take the additive inverse by inverting and then increment by 1.
Additive Inverse of B:
B= 0001
-B= 1110 // Invert
-B= 1111 // Increment +1
Now sum A + (-B) :
A=0000
(+) -B=1111
-----------
A+(-B)=1111
Note that if we are only allowed one bit. We can't represent negative numbers since there's no room for the value.
I need to know how to solve this problem please
Represent the following decimal numbers in binary using 8-bit signed magnitude, one’s complement, and two’s complement:
88
-76
My solution is :
88 = 01011000 8 bit sm
10100111 1s complement
10101000 2s complement
-76 = Not sure about this one
In 8 bit signed magnitude, the MSB denotes the sign of the
number,whether positive or negative.
88 = 01011000
^(MSB) this is signed bit,0 for positive.
In Decimal in order to get -76, we subtract 76 from the number of combinations (256), which gives, 256 - 76 = 180.
-76 = 10110100
^(MSB) this is signed bit, 1 for negative.
For one's complement representation, simply reverse the bits,i.e.,
change 0 to 1 and 1 to 0.
Therefore, 86(one's complement) = 10100111.
And, -76(one's complement) = 01001011.
Also, we obtain two's complement by adding 1 to the binary number
representation of the number.
Therefore, 86(two's complement) = 10101000.
And, -76(two's complement) = 01001100.
This link should help you in solving your problem - it's pretty short and straight-forward:
http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch04s11.html
Short explanation of 8-bit signed magnitude:
Number in your desired format looks like this:
1000 0110, which is equal to -6 in decimal:
1*** **** - means that number HAS sign (which is minus),
*000 0110 - which contains binary representation of number.
When you have a positive number, you simply convert it to it's binary form:
(D) 7 = 0000 0111
(D) 20 = 0001 0100
When you have a negative number (e.g. -7), your highest bit is equal to 1:
1...
and the value is simply converted to binary form:
(D) 7 = 111 = 000 0111
Then you combine it:
(D) -7 = 1000 0111.
Note that in this format you can only save number ranged from -127 to 127 - you have just 7 lower bits left for value whereas 8th bit must be left for a sign.
1s complementary example:
(D) -7 = 1000 0111 in 8-bit signed.
When you add a number and it's 1s complementary, you should get: 1111 1111
1000 0111 - your number
0111 1000 + - your number's 1s complementary
^^^^^^^^^
1111 1111
Formally, you can do the following operation:
1111 1111
1000 0111 - - your number
^^^^^^^^^
... <- your number's 1s complementary
2s complementary:
When you calculated 1s complementary, simply add 1 to it:
1000 0111 - your number
0111 1000 - your number's 1s complementary
0111 1001 - your number's 2s complemetary
Try to do this yourself and post your answer - this way you will learn much more.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
What is 2's Complement Number?
Why do we take 1's Complement and add 1 to it? Why don't we subtract 1 after taking 1's Complement?
Why do computers use 2's Complement?
What is 2's Complement Number?
Complementary number system is used to represent negative numbers. So,
2's Complement number system is used to represent negative numbers.
UPDATE
Q: What “2’s Complement System” says?
A: The negative equivalent of binary number is its 2’s complement. (1’s Complement + 1)
Note: 1 extra bit is required to represent the sign of a number. MSB (Most Significant Bit) is used as sign bit. If MSB is 0, then the number is positive. If MSB is 1, then the number is negative.
1’s Complement Value 2’s Complement
011 +3 011
010 +2 010
001 +1 001
000 +0 000
111 -0 000
110 -1 111
101 -2 110
100 -3 101
-4 100
How '100' (3 bits) is -4?
MSB is used as sign, if 1, its negative, if 0 it is positive.
-1 * 2^2 + 0*2^1 + 0*2^0 = -4 + 0 + 0 = -4
Similarly 101 (3 bits) is -3
-1 * 2^2 + 0*2^1 + 1*2^0 = -4 + 0 + 1 = -3
Observations:
• In 1’s complement, using 3 bits, we represented 2^3 = 8 numbers i.e from -3 to +3.
• In 1’s complement, -0 and +0 are having 2 representation. (+0 is ‘000’ and -0 is ‘111’).
But mathematically +0 and -0 are same.
• In 2’s complement, using 3 bits, we represented only 2^3 = 8 numbers i.e from -4 to +3.
• In 2’s complement, -0 and +0 are having same representation.
• Since +0 and -0 in 2’s complement is having same representation,
we are left out with one more combination which is ‘100’ = -4.
Why do we take 1's Complement and add 1 to it? Why don't we subtract 1 after taking 1's Complement?
Refer "Why Inversion and Adding One Works" topic in the below link. If
I start explaining, this post will grow big.
http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html
Why computer uses 2' Complement?
Cos' of less hardware. If the computer is using 2' Complement means,
it does subtraction using addition circuit. So, less hardware!!!
As seen in the above example, +0 and -0 have same representation. (1's complement and sign magnitude representation have 2 different representation for +0 and -0).
(Not an important) You will be able to represent one extra number using 2's complement. (in the above example its -4 which is '100' in binary using 3 bits).
From Wikipedia Two's Complement :
The two's-complement system has the advantage that the fundamental arithmetic operations of addition, subtraction, and multiplication are identical to those for unsigned binary numbers (as long as the inputs are represented in the same number of bits and any overflow beyond those bits is discarded from the result). This property makes the system both simpler to implement and capable of easily handling higher precision arithmetic. Also, zero has only a single representation, obviating the subtleties associated with negative zero, which exists in one's-complement systems.
The reason why we use two's complement rather than one's complement is to make arithmetic as simple as possible.
Consider that in one's complement, 1111 1111 and 0000 0000 are the same number - by subtracting one, we have ended up with...the same number. This is too big of a pain to have to think about - so instead we use two's complement, where 1111 1111 is -1 - by subtracting one, we successfully subtract one. Hooray!
(A secondary advantage is that we can represent one more unique number in two's complement than in one's complement — in two's complement -128 to +127 instead of one's complement -127 to +127.)
The 2's complement of a number which is represented by N bits is 2^N-number.
For example: if number is 7 (0111) and i'm representing it using 4 bits then, 2's complement of it would be (2^N-number) i.e. (2^4 -7)=9(1001)
7==> 0111
1's compliment of 7==> 1000
1000
+ 1
-------------
1001 =====> (9)
While calculating 2's complement of a number, we do following steps:
1. do one's complement of the number
2. Add one to the result of step 1.
I understand that we need to do one's complement of the number because we are doing a negation operation. But why do we add the 1?
This might be a silly question but I'm having a hard time understanding the logic. To explain with above example (for number 7), we do one's complement and get -7 and then add +1, so -7+1=-6, but still we are getting the correct answer i.e. +9
Your error is in "we do one's compliment and get -7". To see why this is wrong, take the one's complement of 7 and add 7 to it. If it's -7, you should get zero because -7 + 7 = 0. You won't.
The one's complement of 7 was 1000. Add 7 to that, and you get 1111. Definitely not zero. You need to add one more to it to get zero!
The negative of a number is the number you need to add to it to get zero.
If you add 1 to ...11111, you get zero. Thus -1 is represented as all 1 bits.
If you add a number, say x, to its 1's complement ~x, you get all 1 bits.
Thus:
~x + x = -1
Add 1 to both sides:
~x + x + 1 = 0
Subtract x from both sides:
~x + 1 = -x
The +1 is added so that the carry over in the technique is taken care of.
Take the 7 and -7 example.
If you represent 7 as 00000111
In order to find -7:
Invert all bits and add one
11111000 -> 11111001
Now you can add following standard math rules:
00000111
+ 11111001
-----------
00000000
For the computer this operation is relatively easy, as it involves basically comparing bit by bit and carrying one.
If instead you represented -7 as 10000111, this won't make sense:
00000111
+ 10000111
-----------
10001110 (-14)
To add them, you will involve more complex rules like analyzing the first bit, and transforming the values.
A more detailed explanation can be found here.
Short answer: If you don't add 1 then you have two different representations of the number 0.
Longer answer: In one's complement
the values from 0000 to 0111 represent the numbers from 0 to 7
the values from 1111 to 1000 represent the numbers from 0 to -7
since their inverses are 0000 and 0111.
There is the problem, now you have 2 different ways of writing the same number, both 0000 and 1111 represent 0.
If you add 1 to these inverses they become 0001 and 1000 and represent the numbers from -1 to -8 therefore you avoid duplicates.
I'm going to directly answer what the title is asking (sorry the details aren't as general to everyone as understanding where flipping bits + adding one comes from).
First let motivate two's complement by recalling the fact that we can carry out standard (elementary school) arithmetic with them (i.e. adding the digits and doing the carrying over etc). Easy of computation is what motivates this representation (I assume it means we only 1 piece of hardware to do addition rather than 2 if we implemented subtraction differently than addition, and we do and subtract differently in elementary school addition btw).
Now recall the meaning of each of the digit's in two's complements and some binary numbers in this form as an example (slides borrowed from MIT's 6.004 course):
Now notice that arithmetic works as normal here and the sign is included inside the binary number in two's complement itself. In particular notice that:
1111....1111 + 0000....1 = 000....000
i.e.
-1 + 1 = 0
Using this fact let's try to derive what the two complement representation for -A should be. So the problem to solve is:
Q: Given the two's complement representation for A what is the two's complement's representation for -A?
To do this let's do some algebra using values we know:
A + (-A) = 0 = 1 + (-1) = 11...1 + 00000...1 = 000...0
now let's make -A the subject expressed in terms of numbers expressed in two's complement:
-A = 1 + (-1 - A) = 000.....1 + (111....1 - A)
where A is in two's complements. So what we need to compute is the subtraction of -1 and A in two's complement format. For that we notice how numbers are represented as a linear combination of it's bases (i.e. 2^i):
1*-2^N-1 + 1 * 2^N-1 + ... 1 = -1
a_N * -2^N-1 + a_N-1 * 2^N-1 + ... + a_0 = A
--------------------------------------------- (subtract them)
a_N-1 * -2^N-1 + a_N-1 -1 * 2^N-1 + ... + a_0 -1 = A
which essentially means we subtract each digit for it's corresponding value. This ends up simply flipping bits which results in the following:
-A = 1 + (-1 - A) = 1 + ~ A
where ~ is bit flip. This is why you need to bit flip and add 1.
Remark:
I think a comment that was helpful to me is that complement is similar to inverse but instead of giving 0 it gives 2^N (by definition) e.g. with 3 bits for the number A we want A+~A=2^N so 010 + 110 = 1000 = 8 which is 2^3. At least that clarifies what the word "complement" is suppose to mean here as it't not just the inverting of the meaning of 0 and 1.
If you've forgotten what two's complement is perhaps this will be helpful: What is “2's Complement”?
Cornell's answer that I hope to read at some point: https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html#whyworks
X = 01001001 and Y = 10101010
If I want to add them together how do I do that? They are "Two's Complement"...
I have tried a lots of things but I am not quite sure I am getting the right answer since there seems to be different type of rules.
Just want to make sure it is correct:
1. Add them as they are do not convert the negative
2. Convert the negative number you get and that's the sum.
f.eks
01001001+10101010 = 11110011 => 00001100 => 1101 => -13
Or?
1. Convert the negative
2. Add them together and convert the negative
f.eks
01001001+10101010 => 01001001 + 01010110 => 10011111 => 01100001 => -97
So basically what I want to do is to take: X-Y, and X+Y
Can someone tell me how to do that?
Some resource sites:
student-binary
celtickane
swarthmore
The beauty of two's complement is that at the binary level it's a matter of interpretation rather than algorithm - the hardware for adding two signed numbers is the same as for unsigned numbers (ignoring flag bits).
Your first example - "just add them" - is exactly the right answer. Your example numbers
01001001 = 73
10101010 = -86
So, the correct answer is indeed -13.
Subtracting is just the same, in that no special processing is required for two's complement numbers: you "just subtract them".
Note that where things get interesting is the handling of overflow/underflow bits. You can't represent the result of 73 - (-86) as an 8-bit two's complement number...
Adding in two's complement doesn't require any special processing when the signs of the two arguments are opposite. You just add them as you normally would in binary, and the sign of the result is the sign you keep.
And just to make sure you understand two's complement, to convert from a positive to a negative number (or vice versa): invert each bit, then add 1 to the result.
For example, your positive number X = 01001001 becomes 10110101+1=10110110 as a negative number; your negative number Y = 10101010 becomes 01010101+1=01010110 as a positive number.
To subtract Y from X, negate Y and add. I.E. 01001001 + 01010110.
Your confusion might be because of the widths of the numbers involved. To get a better feel for this you could try creating a signed integer out of your unsigned integer.
If the MSB of your unsigned integer is already 0, then you can read it as signed and get the same result.
If the MSB is 1 then you can append a 0 to the left to get a signed number. You should sign-extend (that is, add 0s if the MSB is 0, add 1s if the MSB is 1) all the signed numbers to get a number of the same width so you can do the arithmetic "normally".
For instance, using your numbers:
X = 01001001: Unsigned, MSB is 0, do nothing.
Y = 10101010: Signed, did nothing with X, still do nothing.
But if we change the MSB of X to 1:
X = 11001001: Unsigned, MSB is 1, Add a 0 --> 011001001
Y = 10101010: Signed, extended X, so sign-extend Y --> 110101010
Now you have two signed numbers that you can add or subtract the way you already know.
01001001 + 10101010 = 11110011 => 00001100 => 1101 => -13
The first addend is 73. The second addend is -86. 86 = 101010. Padding to 8 bits including the 1 for the negative sign, -86 = 10101010
Both addends are in Sign-bit representation.
Solving them their sum is 1 1 1 1 0 0 1 1 which is an encoded binary (equivalent to having undergone One's Complement by inversion then Two's Complement by adding 1).
So do the reverse to have the decimal number. This time do first subtract 1 as inverse of Two's Complement = 1 1 1 1 0 0 1 1 - 1
= 1 1 1 1 0 0 1 0 then invert as in One's Complement = 0 0 0 0 1 1 0 1 which is equal to 13. Having done such reversal or having acknowledged One's complement and Two's Complement, the answer is negative. So affix the negative sign = -13