问题
What is the maximum and minimum values can be represented with 5-digit number that is assuming 2's complement representation?
do I find the the min and maximum value of 5-digit numbers, which are 00000 and I'm not sure what the max is. Then convert to two's complement? This sounds stupid, but it's the only one I can come up with...
my last question is: What is the minimum register length in a processor required to store values between –EA(base16) and 24(base16) assuming they are stores using the 2’s complement format?
I'm not sure how to attack this problem.
Any help or explanation would be really appreciated :)
回答1:
Two's complement
The two's complement range with N
digits is −(2N − 1 − 1)
to +(2N - 1)
.
That happens because to obtain the two's complement representation of number you:
- Keep it as is if the number is greater than or equal to zero.
- Invert all the bits and add one if the number is less than zero.
So the first bit (MSB) will be a sign bit and will be equal to one when the number is negative. If you try to convert a negative number and end up with a number starting in zero (or if you you try to convert a positive one and end up with a number starting in one) you'll need to get more bits to store this number properly.
With that in mind when N
is equal to 5:
Maximum
(25 - 1)
= (24 - 1)
= 16 - 1
= 15
Minimum
−(25 − 1 − 1)
= −(24 − 1)
= −(16)
= -16
If you add one the greatest possible number (15)10 = 011112 you'll get to 10000 2 = -16 and that's why this are the maximum/minimum values possible.
Converting Hex to Binary digit by digit:
-EA
-EA16 = -(1110 1010)2
If this number were only 8 bits long
- The first bit would be the sign bit.
To get the two's complement you must invert all bits and add 1:
-EA16 = (0001 0101 +1)2 = 0001 01102
Doing that you realise that you need another bit to store your sign because the number you got seems to be positive! (negative numbers always start with one in this representation, we know this number is negative but it starts with a zero).
Converting the obtained number to decimal we get 22, which is positive. This issue happened because we did not add a bit to represent the sign.
With the aditional sign bit:
If it were 9 bits long
-EA16 = -(0 1110 1010)2 (1 0001 0101 +1)2 = 1000 01102
So you'll need 9 bits to store that number.
24
2416 = (0010 0100)2
To store thus number properly you'll only need 7 bits (6 bits + sign bit).
Register total number of bits
You'll need a 9 bit register since you should be able to store both numbers. (You have to get the size of the biggest number otherwise it will be truncated and wrongly represented.)
9 bit register
Even though a bit unusual a 9 bit register can store numbers in the following range:
Maximum
(29 - 1)
= (28 - 1)
= 256 - 1
= 255
= FF16
Minimum
−(29 − 1 − 1)
= −(28 − 1)
= −(256)
= -256
= -10016
As we can see -EA > -100
and 24 < FF
so the numbers can be stored!
回答2:
I want to clarify on this old answer.
The two's complement range with N
digits is −(2N − 1)
to +(2N - 1 - 1)
.
This is different from the first line of the top voted answer.
With that in mind when N
is equal to 5:
Maximum
(25 - 1 - 1)
= (24 - 1)
= 16 - 1
= 15
Minimum
−(25 − 1)
= −(24 − 1)
= −(16)
= -16
来源:https://stackoverflow.com/questions/28116442/what-is-the-maximum-and-minimum-values-can-be-represented-with-5-digit-number-i