top of page
  • Writer's pictureJeffrey Scholz

Uint256 max value

Updated: Apr 7

The uint256 max value can be obtained with type(uint256).max; which is 115792089237316195423570985008687907853269984665640564039457584007913129639935 or 2^256-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;

The math behind maximum values in integers and unsigned integers

For unsigned integers, just put 2 ** n - 1 into the terminal with your favorite interpreted language to get the answer, where n is the size of the uint in question, for example uint128 or uint32 (or even rarely used but valid sizes like uint208). A uintN means there are N bits that represent the number, and when all of them are set to 1, this is the maximum binary representation.


The EVM uses Twos Complement to represent signed numbers, so to get the the maximum value of a signed integer, the formula is 2 ^ (N - 1) - 1 and the most negative value is -2 ^ (N - 1).


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;
}

It returns the correct value, which you can verify with

assert(2**256 - 1 == type(uint256).max);

If you write 2**256 as a constant in solidity, the code won't compile because the compiler recognized 2**256 is too large to fit in uint256. But if it is immediately followed by a "- 1", then the compiler recognized the result of the arithmetic is valid with the type.


It's better to just avoid all these gymnastics and just do type(uint256).max;


Learn more

If you are new to Solidity, see our learn solidity free for beginner course.

For intermediate and advanced developers please see our expert solidity bootcamp.

348 views0 comments

Recent Posts

See All

We will demonstrate a step-by-step exploration of a basic zk-dapp designed for verifying additions. This application enables users to prove that the sum of two numbers, X and Y, equals Z without discl

bottom of page