Uint256 max value

Jeffrey Scholz
RareSkills
Published in
1 min readApr 5, 2023

--

The maximum value of uint256 can be obtained with

type(uint256).max;

Which is equal to 115792089237316195423570985008687907853269984665640564039457584007913129639935 (2²⁵⁶ — 1). But it’s cleaner and safer to use type(uint256).max.

The same can be used for signed integer types

//57896044618658097711785492504343953926634992332820282019728792003956564819967
type(int256).max;

//-57896044618658097711785492504343953926634992332820282019728792003956564819968
type(int256).min;

Hacky ways to get the uint256 maximum value

You could also specify it with hexadecimal, which would be a little cleaner than using the decimal representation, but still space consuming and error prone.

0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

That’s exactly 64 ‘f’s, don’t get it wrong! The 64 is derived from 256 bits divided by 8 to get the number of bytes (32), and each maximum byte is represented in hex by 0xff.

Another ugly solution is to underflow the integer

function maximum() public pure returns(uint256) {
unchecked { return uint256(0) - uint256(1); }
}

The following code works properly, but it is deceptive and not recommended for use

function maxUint() public pure returns (uint256) {
return 2**256 - 1;
}

--

--