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?
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^N-1
. –
Lysander 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...
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.
Integer.MAX_VALUE
--for 32 bit integers--is 2^31 - 1 and Long.MAX_VALUE
--for 64 bit integers--is 2^63 - 1 because the sign bit is reserved.) –
Marcellus char
excluded). Refer to the Wikipedia article on Two's complement and you can read all about the sign bit. Also refer to the Wikipedia article on Sign bit. –
Marcellus In most programming languages, 0
is a number too.
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.
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)
.
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.
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.
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?
Why do we have the "minus 1"?
Just answer the question: What is the maximum value of an 1-bit integer?
One bit integer can store only two (21) values: 0
and 1
. Last value is 12 = 110
Two bit integer can store only four (22) values: 00
, 01
, 10
and 11
. Last value is 112 = 310
Thus, when integer can stores N
, values last value will be N-1
because counting starts from zero.
n
bit integer can store 2
n
values. Where last will be 2
n-1
Example:
One byte can store 2
8 (256) values. Where first is 0
and last is 255
Why isn't the maximum just 2n?
Because counting starts from zero. Look at first value for any n
bit integer.
For example byte: 00000000
This would be very confusing if:
00000001
means 2
00000000
means 1
would not? ;-)
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 number1
=> negative number
Then the maximum value for 32-bit
number is limited by 231. And adding -1
we get 231 - 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 <-- the most right bit cannot represent 2
--- --------------------
128 + 127 = 255
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)
In the field of computing we start counting from 0.
© 2022 - 2024 — McMap. All rights reserved.