VelvetShark

Max values for each uint in Solidity, from uint8 to uint256

Published on

Sream by Darren Almond

If you often struggle to remember which uint has what max value in Solidity, as I do, this is a cheatsheet for you (and for me, I'll probably use it all the time).

Gas is expensive on Ethereum so it's best to optimize for it and not to use variables that are larger than what we need. If all we are going to store in a variable are numbers from 1 to max 100, it doesn't make sense to use uint256. It's a waste of gas and money.

I always struggle to remember what's the maximum value for each uint, so I've created a cheatsheet. Now I won't have to worry if my chosen uint is going to be enough or too small.

Below, (almost) all the max values for each uint. I've added the number of digits for each value, so for example, if you know that your max value will have no more than 12 digits, you can safely choose uint40, which has 13 digits.

I didn't include anything between uint128 and uint256. If you're going to use such large numbers, you'll probably use uint256 by default.

A few things to know/remember:

  • uint increases in steps of 8, from uint8 to uint256
  • For an integer type X, you can use type(X).min and type(X).max to access the minimum and maximum value representable by the type
  • uint is an alias for uint256
uint Digits Max value
-----------------------------
uint8 3 255
uint16 5 65,535
uint24 8 16,777,215
uint32 10 4,294,967,295
uint40 13 1,099,511,627,775
uint48 15 281,474,976,710,655
uint56 17 72,057,594,037,927,935
uint64 20 18,446,744,073,709,551,615
uint72 22 4,722,366,482,869,645,213,695
uint80 25 1,208,925,819,614,629,174,706,175
uint88 27 309,485,009,821,345,068,724,781,055
uint96 29 79,228,162,514,264,337,593,543,950,335
...
uint128 39 340,282,366,920,938,463,463,374,607,431,768,211,455
...
uint256 78 115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,665,640,564,039,457,584,007,913,129,639,935

Now your uints will always be within range and will never take up more space than needed. Happy savings!