LogicGates.org Open the simulatorSimulator

uint32: the 32-bit unsigned integer

uint32 holds every whole number from 0 to 4,294,967,295, which is 0 to 2³² − 1. That is 2³² = 4,294,967,296 values in 32 bits, none of them negative.

uint32 limits

Maximum
4,294,967,295
power2³² − 1
hex0xFFFFFFFF
binary1111 1111 1111 1111 1111 1111 1111 1111
Minimum
0
power0
hex0x00000000
binary0000 0000 0000 0000 0000 0000 0000 0000
Distinct values
2³² = 4,294,967,296
Size
32 bits, 4 bytes

uint32 overflow playground

Starts at the maximum, one step from overflowing. Change the operation, the value or the type, or cast to another width, and watch the bits wrap.

Decimal, 0x hex or 0b binary, with a minus sign if negative. 2^31 − 1 works too.

uint32: 4,294,967,295 +1 0 Overflow: wrapped past the maximum to the bottom of the range
Before 0xFFFFFFFF
Exact answer 4,294,967,296 written in 33 bits: the leftmost bit does not fit and is dropped
After 0x00000000

Struck-through bits are dropped.

  1. Exact answer: 4,294,967,296.
  2. That is above the maximum of uint32, 4,294,967,295.
  3. The hardware keeps only the low 32 bits, which is the same as taking the answer modulo 2³² = 4,294,967,296: 0.

What uint32 is called in each language

Language Type Limits in code Note
C and C++ uint32_t UINT32_MAX
Java none Use int with Integer.toUnsignedLong, Integer.divideUnsigned and friends.
C# uint uint.MaxValue
Rust u32 u32::MAX
Go uint32 math.MaxUint32
Swift UInt32 UInt32.max
Kotlin UInt UInt.MAX_VALUE
MySQL INT UNSIGNED
PostgreSQL none PostgreSQL has no unsigned integer types; bigint holds the whole range.
SQL Server none SQL Server has no unsigned types apart from tinyint.
JavaScript Uint32Array element x >>> 0 reads a number as a 32-bit unsigned integer.
Python ctypes.c_uint32

Only names with exactly 32 bits on every platform are listed as the type. C’s int, whose width depends on the platform, is in the notes.

What happens when uint32 overflows

In 32 bits, 4,294,967,295 + 1 wraps to 0, and 0 − 1 wraps to 4,294,967,295. The processor keeps the low 32 bits of the answer, which is the same as working modulo 2³². You can see it in JavaScript:

(4294967295 + 1) >>> 0 → 0

Whether a program is allowed to wrap like that depends on the language:

What each language does when integer arithmetic overflows
Language By default To check or wrap on purpose
C and C++ Unsigned types wrap around modulo 2ⁿ. Signed overflow is undefined behaviour: the compiler may assume it never happens and optimise on that basis. Types narrower than int are promoted to int first, so even uint16_t × uint16_t can overflow a signed int. __builtin_add_overflow (GCC, Clang); ckd_add in C23’s <stdckdint.h>; -fsanitize=undefined to catch it
Java Wraps around silently, in two’s complement. Math.addExact, multiplyExact and friends throw ArithmeticException
C# Wraps around, unless the code is in a checked context, which throws OverflowException. A constant expression that overflows is a compile error. checked(...) blocks, or the CheckForOverflowUnderflow compiler option
Rust Panics in debug builds. Release builds wrap, unless overflow-checks is turned on for that profile. checked_add, wrapping_add, saturating_add, overflowing_add
Go Wraps around silently. Only constant expressions that overflow are compile errors. math/bits.Add64 reports the carry; Mul64 returns the high half of the product
Swift Traps: the program stops with a runtime error. &+, &-, &* wrap on purpose; addingReportingOverflow reports it
Kotlin Wraps around silently, like Java. Math.addExact on the JVM
JavaScript Numbers never wrap, but lose precision past 2⁵³. Bitwise operators and typed arrays wrap to their width. BigInt never overflows. Number.isSafeInteger; BigInt.asIntN and asUintN to wrap deliberately
Python int never overflows: it grows as needed. ctypes and NumPy fixed-width types do wrap
SQL An error: the statement fails with an out of range or arithmetic overflow message. MySQL outside strict mode clips an out-of-range value stored into a column to the limit, with a warning

Common mistakes with uint32

  • Counting down to zero with an unsigned counter. A loop such as for (i = n; i >= 0; i--) never ends when i is uint32: i >= 0 is always true, and 0 − 1 wraps to 4,294,967,295. Test i > 0 and use i − 1 inside, or use a signed counter.
  • Comparing signed with unsigned in C. -1 < x is false when x is a uint32_t, even when x is 0: the −1 is converted to uint32 first and becomes 4,294,967,295 (on platforms where int is 32 bits).
  • Finding a midpoint as (low + high) / 2. When low and high are both 2,147,483,648 or more, just over half the maximum, their sum overflows uint32. low + (high − low) / 2 gives the same midpoint without the overflow.

When uint32 overflowed for real

Windows 95 and 98 after 49.7 days

Microsoft documented that Windows 95 and 98 could stop responding after exactly 49.7 days of continuous running, a fault in their timing code. 2³² milliseconds is 49.71 days, the point where a 32-bit count of milliseconds runs out.

Unsigned Unix time

Stored as unsigned 32 bits, Unix time runs out later than the signed 2038 limit: the last second is 2106-02-07 06:28:15 UTC. The price is that dates before 1970 cannot be written at all.

Where you meet uint32

Need negative numbers? The signed type of the same width is int32, from −2,147,483,648 to 2,147,483,647.

Every integer type

Type Minimum Maximum
int8 −128 127
uint8 0 255
int16 −32,768 32,767
uint16 0 65,535
int32 −2,147,483,648 2,147,483,647
uint32 this page 0 4,294,967,295
int64 −9,223,372,036,854,775,808 9,223,372,036,854,775,807
uint64 0 18,446,744,073,709,551,615
int128 −170,141,183,460,469,231,731,687,303,715,884,105,728 170,141,183,460,469,231,731,687,303,715,884,105,727
uint128 0 340,282,366,920,938,463,463,374,607,431,768,211,455

Questions

What is the maximum value of uint32?

4,294,967,295, which is 2³² − 1 or 0xFFFFFFFF in hex. The minimum is 0.

Why does uint32 start at 0?

An unsigned type spends all 32 bits on magnitude and none on a sign, so it cannot hold negative numbers but reaches twice as far: 4,294,967,295 instead of the 2,147,483,647 of int32. Subtracting 1 from 0 wraps to the maximum.

How many values can uint32 hold?

2³² = 4,294,967,296 distinct values, from 0 to 4,294,967,295.

What happens when uint32 overflows?

On the hardware the result keeps only its low 32 bits, so 4,294,967,295 + 1 becomes 0. Kotlin, Go and C# (outside a checked context) do exactly that. C and C++ wrap unsigned types the same way; Rust panics in debug builds and wraps in release builds; Swift stops with a runtime error.

What is uint32 called in other languages?

C and C++: uint32_t; C#: uint; Rust: u32; Go: uint32; Swift: UInt32; Kotlin: UInt; Python: ctypes.c_uint32. In SQL: INT UNSIGNED in MySQL.