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

匿名 (未验证) 提交于 2019-12-03 08:41:19

问题:

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?

回答1:

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).



回答2:

2^32 in binary:

1 00000000 00000000 00000000 00000000 

2^32 - 1 in binary:

11111111 11111111 11111111 11111111 

As you can see, 2^32 takes 33 bits, whereas 2^32 - 1 is the maximum value of a 32 bit integer.

The reason for the seemingly "off-by-one" error here is that the lowest bit represents a one, not a two. So the first bit is actually 2^0, the second bit is 2^1, etc...



回答3:

232 in binary is one followed by 32 zeroes, for a total of 33 bits. That doesn't fit in a 32-bit int value.



回答4:

In most programming languages, 0 is a number too.



回答5:

The numbers from 0 to N are not N. They are N+1. This is not obvious to the majority of people and as a result many programs have bugs because if this reason.



回答6:

If you're just starting out with programming, I suggest you take a look at this wiki article on signed number representations

As Vicente has stated, the reason you subtract 1 is because 0 is also an included number. As a simple example, with 3 bits, you can represent the following non-negative integers

0 : 000 1 : 001 2 : 010 3 : 011 4 : 100 5 : 101 6 : 110 7 : 111 

Anything beyond that requires more than 3 digits. Hence, the max number you can represent is 2^3-1=7. Thus, you can extend this to any n and say that you can express integers in the range [0,2^n -1]. Now you can go read that article and understand the different forms, and representing negative integers, etc.



回答7:

It's because in computing, numbers start at 0. So if you have, for example, 32 address lines (232 addressable bytes), they will be in the range [0, 2^32).



回答8:

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?



回答9:

In most programming languages integer is a signed value (see two's complement).

For example, in Java and .NET integer most left byte is reserved for sign:

  • 0 => positive or zero number
  • 1 => negative number

Then the maximum value for 32-bit number is limited by 2^31. And adding -1 we get 2^31 - 1.

Why does -1 appear?

Look at more simple example with unsigned Byte (8-bits):

  1  1  1  1  1  1  1  1 128 64 32 16  8  4  2  1  

As other guys pointed out the most right bit can have a maximum value of 1, not 2, because of 0/1 values.

Int32.MaxValue = 2147483647 (.NET) 


回答10:

Because 0 is also represented. The amount of numbers you can represent is indeed 2^n with n bits, but the maximum number is 2^n-1 because you have to start the count in 0, that is, every bit set to 0.

For 1 bit: 0, 1
For 2 bits: 0, 1, 2, 3
For 3 bits: 0, 1, 2, 3, 4, 5, 6, 7

And so on.



回答11:

In the field of computing we start counting from 0.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!