Why is the maximum value of an unsigned n-bit integer 2^n-1 and not 2^n?

后端 未结 12 1219
萌比男神i
萌比男神i 2020-11-28 06:01

The maximum value of an n-bit integer is 2n-1. Why do we have the \"minus 1\"? Why isn\'t the maximum just 2n?

12条回答
  •  猫巷女王i
    2020-11-28 06:43

    The -1 is because integers start at 0, but our counting starts at 1.

    So, 2^32-1 is the maximum value for a 32-bit unsigned integer (32 binary digits). 2^32 is the number of possible values.

    To simplify why, look at decimal. 10^2-1 is the maximum value of a 2-digit decimal number (99). Because our intuitive human counting starts at 1, but integers are 0-based, 10^2 is the number of values (100).

提交回复
热议问题