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.
Related
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
For example, 10000001 is -127 in Two's Complement, but it's also equal to 129. How do I or a computer know which number it's referring to?
From your question, I deduce you already know you are dealing with a signed integer. So, your real question is: how do we convert a two's complement representation to a decimal number?
For demonstration purposes, I will be using 4-bit integers.
A two's complement number system encodes positive and negative numbers in a binary number representation. The weight of each bit is a power of two, except for the most significant bit (leftmost bit), whose weight is the negative of the corresponding power of two.
Example: Let's convert these two's complement numbers: 1110,0111,1001.
1110= -23 + 22 + 21 + 0*20 = -2
0111= -23*0 + 22 + 21 + 20 = 7
1001= -23 + 0*22 + 0*21 + 20 = -7
Remark: The leftmost bit gives you the sign of the number. That's, if it's "0", then the number is positive; if not, then it's negative.
So, to come back to the example you gave:
10000001 = -27 + 0 + 0 + 0 + 0 + 0 + 0 + 1= -127 (and not 129)
So, how do you represent 129 in two's complement?
To represent 129 in two's complement, you need more than 8 bits, since an additional bit is needed for the computer to know the number is positive. Not clear? Let's assume we are in a 16-bit system, here is how -127 and 129 would be stored:
-127= 10000001
129= 00000000 10000001 (The leftmost bit being 0 implies the number is positive)
In case you still doubt whether or not 129 can be represented using 8 bits in two's complement: Recall that on an n-bit system, the values that can be represented in two's complement lie in the interval [-2n-1,2n-1-1].Here, n=8 bits. Thus, the interval is [-128,127], which clearly shows that 129 is out of range.
Source: https://en.wikipedia.org/wiki/Two's_complement
How do I or a computer know which number it's referring to?
It depends on whether the given number is signed or unsigned.
Signed
Signed values can be positive or negative. Computers use the first bit to indicate this. If the first bit is 1, the number is negative. If the first bit is 0, the number is positive. To determine the value of the number, the other 7 bits are used.
Hence for a signed 1000 0001, it will mean -1
Unsigned
Unsigned values will only be positive. Since there is no indication needed for whether the number is positive or negative, all 8 bits will be used to determine the value of the number.
For an unsigned 1000 0001, it will mean 129
For signed integer 8 bit representations, the most significant bit represents the sign. Hence, you have 7 bits to represent the value itself. For unsigned integer 8 bit representations, all 8 bits are used to represent the value, as the numbers in this representation are already assumed to be positive.
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.
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!