Every integer type stores a fixed number of bits, and the number of bits sets the range of values it can hold. The integer range calculator takes a number of bits and a whole number and gives the largest unsigned value, the signed range, the number of distinct values, the storage size and the bits your number needs, with a note on whether it fits.
How the calculation works
- Largest unsigned value = 2^n − 1, where n is the number of bits. All the bits carry the value, so the range runs from 0 to 2^n − 1.
- Signed range (two’s complement) = −2^(n−1) to 2^(n−1) − 1. One bit is used for the sign, so there is one more negative value than positive values.
- Number of distinct values = 2^n, and the storage size is n ÷ 8 bytes.
- Bits needed for your number = the smallest n such that 2^n > your number. A signed integer needs one more bit than an unsigned one, to hold the sign.
Example
A 32-bit integer has 2^32 = 4,294,967,296 distinct values. As an unsigned integer it runs from 0 to 4,294,967,295, and as a signed integer from −2,147,483,648 to 2,147,483,647. The number 3,000,000,000 needs 32 bits as an unsigned integer, so it fits, but it needs 33 bits as a signed integer, so it does not fit in a signed 32-bit variable and wraps around to a negative value.
Why it matters
Choosing a type that is too small leads to overflow bugs, and choosing one that is too large wastes memory and bandwidth. The limits are worth checking when you design a database column, a file format, a network protocol, an embedded program or a game with large scores.
Good to know
The calculator assumes two’s complement, which is used by virtually all current hardware. Sign-and-magnitude and one’s complement formats have two zeros and a different negative limit. For very large numbers, the values shown above 2^53 are displayed in rounded form, so rely on the formulas for exact limits.
Related tools
The parity bit calculator shows how an extra bit detects an error and the Shannon entropy calculator measures how many bits of information a source really carries.