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

后端 未结 12 1198
萌比男神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条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 06:40

    If I ask you what is the biggest value you can fit into a 2-digit number, would you say it's 102 (100) or 102-1 (99)? Obviously the latter. It follows that if I ask you what the biggest n-digit number is, it would be 10n-1. But why is there the "-1"? Quite simply, because we can represent 0 in a 2-digit number also as 00 (but everyone just writes 0).

    Let's replace 10 with an arbitrary base, b. It follows that for a given base, b, the biggest n-digit number you can represent is bn-1. Using a 32-bit (n = 32) base-2 (b = 2) number, we see that the biggest value we can represent 232-1.


    Another way of thinking about it is to use smaller numbers. Say we have a 1-bit number. Would you tell me the biggest value it can represent is 21 or 21-1?

提交回复
热议问题