What is the maximum and minimum values can be represented with 5-digit number? in 2's complement representation

前端 未结 2 946
温柔的废话
温柔的废话 2021-01-15 14:02

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 val

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-15 14:40

    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!

提交回复
热议问题