So i was told that two's complement is usually to used to find complement of a number and I used it only to complement positive numbers (i.e positve --> negative conversion) however I just got an example in book which asks me the following :
Express 23, -23, and -9 in 8-bit binary two’s Complement
form
now what does that mean? 23 means -23 in binary and -23 means 23 ?
sorta confused over there
2's complement is used to represent negative numbers, which in turn, can be used to do subtraction.
23 = 00010111b
To get -23 (2's complement of 23), flip all the bits, and add 1:
11101000b + 1
=11101001b (-23)
-9 is the 2's complement of 9. 9 is
00001001b
So -9 is
11110111b (Flipping and add +1)
See also here
The representation of positive numbers in 2's complement is same as the unsigned representation. Things start to change when negative representation comes into play. So, in general, for given w bits, the numbers that can be represented in 2's complement are -2^(w-1)-1 to 2^(w-1) with the w bit being the signed bit. So since you have 8 bits you can represent numbers from -128 to 127. The 8th bit will be the signed bit, with 0 being positive and 1 representing negative.
For any positive representation of a number in binary 2's complement N the negative counterpart -N is pretty simple to find, just invert the bits, and add one. Example:
7 in 2's complement is 0111, inverting these bits gives: 1000, adding one gives 1001, which is -7 in 2's complement! Hope this helps!
Related
What is the most negative number we can represent with 7-bit two's complement representation?
With 7-bits of 2's complement, it could range from -64 to 63. (traditionally, 7 bits can only go up to 2^n-1 which is 128 but MSB is reserved for sign, so we could have 6 bits to represent the data. We will be getting 64 positive and 63 negative values and answer should be -64, 63.)
No, because in two's complement, the most significant bit is the sign bit. 0000001 is +1, a positive number.
That is why the range of two's complement 7-bit numbers is -64 to 63, because 64 is not representable (it would be negative number otherwise).
The most negative number is 1000000. The leading 1 tells you it's negative, and to get the magnitude of the number, you flip all the bits (0111111), then add one (1000000 = 64). So the resulting number is -64 thru 63.
using 4-bit numbers calculating -3 + -3 in two's complement, my calculation yields -2.
3 in binary is 0011
making both one's complement by flipping all the digits since both are negative
1100 && 1100
adding one to them, since we are using two's complement
1101 + 1101 =
11010
first one is overflow, and are tossed away in two's complement. So are left with 1010, which is minus two in decimal. Can someone explain what is done wrong in this process?
EDIT
My problem seems more specifically how to interpret the result of a two's complement calculation. I treat the result as following:
The result is 1010.
In my world, the first bit is a sign bit, indicating that the number is negative.
The following 0 means there is 0 of -4's
the following 1 means there is 1 of -2's
the following 0 means there is 0 of -1's
therefore, I interpret it like the result is -2
The result is correct, you just interpreted it wrong. When you perform arithmetic on numbers in two's complement, the result is also two's complement. So the conversion from negative to positive is done the same way as the conversion of the original numbers from positive to negative.
Given the 4-bit binary value 1010, first flip the bits for one's complement to get 0101, then add 1 for two's complement to get 0110.
0110 is 6, so 1010 is -6.
first one is overflow
If, as you imply here, you are restricting your storage to a single nybble, then with two's complement, you can represent values in the range -8 to 7.
-2 would then be 0b1110, which is not what you have. -6 is indeed 0b1010, which is the correct sum.
I was converting -15 to Two's complement -
15 in binary = 1111
Flip bits and add 1 = 0001
However I also thought that the leftmost digit of a two's complement number indicates whether it represents a positive (0) or negative (1) number? Need some help understanding this
Using the 2's complement method of subtraction, my result is 1111 (-1 in decimal system) whereas it should be 0 in the following case
10010
-10010
where both values are provided in binary.
The answer seems quite obvious but following the method of conversion of the negative value to 2's complement and then adding it does not seem to work in this case as the result seems to be 1111.
Thanks
Subtracting a number from itself using 2's complement means you add its 2's complement (subtracting a number is adding its 2's complement):
You need to know the word size in bits to determine 2's complemet, so let's suppose the number of bits in a word is 8. Then you have 00010010 as the original number, and 11101110 as its 2's complement. The 2's complement is the 1's complement (11101101) with 1 added to it.
Then adding:
00010010 [original number, 18 decimal]
+11101110 [2's complement, -18 decimal]
---------
00000000 [note: there's a 1 overflow bit]
10010 in twos complement (assuming 5 bit two's complement) is -14 (-16 +2). 14 in twos complement is 01110 (2+4+8). So
10010
-10010
=
10010
+01110
=
00000 (actually 100000, but the 1 is overflowed)
I'm using two's complement and -1(decimal) is converted to 1111 1111(TwoCom) on eigth bits. The first bit says that the number is negative and when I convert to hex I receive a FF by result.
Is correct?
But I need to know how I can represent -1 (decimal) in hex on 9 bits.
I guess that this would be 1FF ... but I'm not sure.
Any help?
A two's complement number is defined as a 1's complement number + 1. So if you have 9 bits available, and you original number is decimal 1, i.e. 000000001 binary, the 1's complement is 111111110 binary, and the 2's complement is 111111111 binary, which represents -1 decimal.
So, you are right, this is 1FF hex.