Skip to content
Omnikalc

Integer range calculator: what values fit in n bits?

Enter a number of bits and a whole number to get the signed and unsigned limits, the number of values, the storage size and whether your number fits.

Updated on
Your inputs

Value out of range

Value out of range

Results

Largest unsigned value

4,294,967,295

Smallest signed value
-2,147,483,648
Largest signed value
2,147,483,647
Number of distinct values
4,294,967,296
Storage size
4.00 bytes
Bits needed for your number (unsigned)
32
Fits as an unsigned integer
Bits needed for your number (signed)
33
Does not fit as a signed integer

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.

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.

Frequently asked questions

Why is there one more negative value than positive values?

In two's complement, zero takes one of the non-negative codes. That leaves 2^(n−1) codes for negative numbers and 2^(n−1) − 1 for positive ones. An 8-bit signed byte runs from −128 to 127, which is why negating the most negative value overflows.

What happens when a number does not fit?

In most languages the value wraps around: 2,147,483,647 plus 1 becomes −2,147,483,648 in a signed 32-bit integer. Some languages raise an error instead. The bug has caused real failures, such as counters that jump to a large negative value after a long run time.

How many bits do common types use?

A byte is 8 bits, a short is usually 16, an int is usually 32 and a long long is 64, but the exact sizes depend on the language and the platform. Python integers have no fixed limit, and JavaScript numbers are doubles that hold whole numbers exactly only up to 2^53.

When should I choose unsigned?

Use unsigned types for quantities that can never be negative, such as sizes, indexes and bit masks, and when you need the extra range. Be careful when mixing signed and unsigned values in an expression, because a negative number converted to unsigned becomes a very large one.

Was this calculator helpful?

Thanks for your feedback!

Results are estimates provided for information purposes. They do not replace professional advice.