What is uint256 in Ethereum?

uint256 is an unsigned integer 256 bits wide — the native word size of the Ethereum Virtual Machine, and the type behind almost every balance, amount and token ID on-chain.

The three parts of the name

u — unsigned

No sign bit is stored, so the value is always zero or positive. That is the right choice for quantities that cannot go below zero — a token balance, an amount of wei, a supply cap. It also means subtracting past zero is an error rather than a negative number (see overflow).

int — integer

Whole numbers only. Solidity has no floating point type, so division truncates toward zero and "decimals" are simulated by scaling: a token with 18 decimals stores 1.5 tokens as 1500000000000000000. The signed counterpart is int256, which uses two's complement and therefore trades one bit of range for the sign.

256 — bits

256 bits is 32 bytes. It is exactly one EVM word and exactly the size of a keccak256 hash, and it comfortably holds a 20-byte address. Narrower words (4 or 8 bytes, as in most CPU architectures) would be too small for hashes and cryptographic values, while arbitrary-precision integers would make gas costs hard to reason about.

Range

Type Minimum Maximum
uint8 0 255
int8 -128 127
uint256 0 115792089237316195423570985008687907853269984665640564039457584007913129639935
int256 -57896044618658097711785492504343953926634992332820282019728792003956564819968 57896044618658097711785492504343953926634992332820282019728792003956564819967

uint8 and int8 both hold 256 distinct values — unsigned starts at zero, signed splits the range around it. The same rule scales up to 256 bits.

Declaring one

uint256 balance;      // 0 .. 2**256 - 1
uint    alsoBalance;  // uint is an alias for uint256
int256  delta;        // signed, two's complement
uint256 max = type(uint256).max;

Sizes come in steps of 8 bits (uint8uint256), and uint is simply an alias for uint256. Since Solidity 0.6.2 the bounds are available as type(uint256).max instead of a hard-coded literal.

Overflow and underflow

// Solidity >= 0.8.0
uint256 a = 0;
a - 1;              // reverts with a panic (underflow)

unchecked {
    a - 1;          // wraps to 2**256 - 1, no revert
}

From Solidity 0.8.0 on, arithmetic is checked: an overflow or underflow reverts the transaction with a panic. An unchecked block opts out and wraps modulo 2256, which is occasionally useful for gas savings in loops. Contracts written before 0.8 relied on OpenZeppelin's SafeMath for the same protection.

Storage and gas

Contract storage is a mapping of 32-byte slots, so a uint256 fills exactly one slot. Smaller types are cheaper only when several of them are declared next to each other and can be packed into one slot — otherwise a uint8 still occupies a whole slot and adds masking instructions on every read and write. As a rule: use uint256 unless you are deliberately packing a struct.

uint256 vs bytes32

bytes32 word  = bytes32(someUint);        // same 32 bytes, no shifting
uint256 value = uint256(someBytes32);

// A short string is right-padded, a number is left-padded:
bytes32 name  = "hello";                 // 0x68656c6c6f00...00
uint256 n     = 0x68656c6c6f;            // 0x0000...68656c6c6f

They are the same 32 bytes and casting between them shifts nothing. The difference is convention: the ABI left-pads numbers and right-pads short strings, so the same bytes mean very different things depending on which side the zeros are on. The converter shows both paddings side by side for exactly this reason.

Wei, gwei and ether

Ether amounts are always integers of wei: 1 ether = 10¹⁸ wei and 1 gwei = 10⁹ wei. A uint256 can hold roughly 1059 ether, which is why the type is never the limiting factor for balances. ERC-20 tokens follow the same idea with their own decimals() value.

Where you will meet it

FAQ

What does uint256 mean?
It is an unsigned integer that is 256 bits (32 bytes) wide. Unsigned means there is no sign bit, so it stores whole numbers from 0 upwards and never a negative value.
What is the maximum value of a uint256?
2**256 - 1, which is 115792089237316195423570985008687907853269984665640564039457584007913129639935. The minimum is 0.
Is uint256 the same as bytes32?
Both occupy exactly 32 bytes and casting between them does not move any bits, but they are encoded differently by convention: numbers are left-padded, short strings stored in a bytes32 are right-padded.
Why does Ethereum use 256-bit words?
256 bits is the EVM word size. It is wide enough to hold a keccak-256 hash or a 20-byte address with room to spare, and using one fixed word size keeps the gas model simple.
Can a uint256 overflow?
Since Solidity 0.8.0 arithmetic is checked and an overflow reverts the transaction. Inside an unchecked block it wraps around modulo 2**256, which is also what older contracts using SafeMath guarded against.

Further reading

→ Convert a uint256 value now