Integers & Overflow
Integers are the most important data type in smart contracts: token balances, prices, votes and timestamps are all integers. Solidity offers many sizes and, since version 0.8, protects you against the most dangerous integer bug, overflow.
Sizes and ranges
uintN stores values from 0 to 2^N − 1; intN stores values from −2^(N−1) to 2^(N−1) − 1. N can be any multiple of 8 from 8 to 256. uint and int are aliases for uint256 and int256.
type(T).min and type(T).max give you the limits:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function main() external pure {
console.log("uint8 max:", type(uint8).max);
console.log("uint32 max:", type(uint32).max);
console.log("uint64 max:", type(uint64).max);
console.log("uint256 max:", type(uint256).max);
console.logInt(type(int8).min);
console.logInt(type(int8).max);
}
}uint8 max: 255
uint32 max: 4294967295
uint64 max: 18446744073709551615
uint256 max: 115792089237316195423570985008687907853269984665640564039457584007913129639935
-128
127
Arithmetic operators
| Operator | Meaning | Note |
|---|---|---|
+ - * |
add, subtract, multiply | checked for overflow |
/ |
integer division | rounds toward zero; division by zero reverts |
% |
remainder | sign follows the left operand |
** |
exponentiation | checked |
<< >> |
bit shifts | not checked; bits fall off |
& | ^ ~ |
bitwise and, or, xor, not |
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function main() external pure {
int256 a = -7;
int256 b = 2;
console.logInt(a / b); // rounds toward zero, not down
console.logInt(a % b); // sign of the left operand
console.log("10 ** 3 =", uint256(10) ** 3);
console.log("1 << 10 =", uint256(1) << 10);
console.log("0xF0 & 0x3C =", uint256(0xF0) & 0x3C);
console.log("0xF0 | 0x0F =", uint256(0xF0) | 0x0F);
}
}-3
-1
10 ** 3 = 1000
1 << 10 = 1024
0xF0 & 0x3C = 48
0xF0 | 0x0F = 255
Checked arithmetic (overflow protection)
Before Solidity 0.8, uint8(255) + 1 silently wrapped around to 0. Attackers exploited this to create huge token balances out of nothing. Since 0.8, every arithmetic operation is checked: if the result doesn't fit, the transaction reverts with a panic error, code 0x11.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function main() external pure returns (uint8) {
uint8 level = 250;
level += 5;
console.log("level is now", level);
level += 1; // 256 doesn't fit in uint8
console.log("never printed");
return level;
}
}level is now 255
main() failed: panic 0x11 (arithmetic overflow or underflow)
The first console.log still appears because the logger saw it before the revert happened. The return value is never produced: a revert ends execution and undoes all state changes.
Underflow is checked too. This is a classic bug in balance logic: subtracting more than an account has.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Wallet {
uint256 public balance = 10;
function spend(uint256 amount) external {
balance -= amount; // reverts if amount > balance
}
}
contract Main {
function main() external {
Wallet w = new Wallet();
w.spend(4);
w.spend(7); // 6 - 7 would be negative
}
}main() failed: panic 0x11 (arithmetic overflow or underflow)
Because w.spend(7) reverted, the revert bubbles up and makes main() fail as well. In real contracts you'd check first and give a clearer message (require(amount <= balance, "insufficient balance")); the Errors lesson shows how.
unchecked blocks
Sometimes you know an operation cannot overflow, or you deliberately want wrap-around (for example in hash calculations). An unchecked { … } block turns the checks off, which also saves a little gas:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function main() external pure {
uint8 x = 255;
unchecked {
x += 1; // wraps around
}
console.log("wrapped:", x);
uint256 zero = 0;
uint256 huge;
unchecked {
huge = zero - 1; // wraps to the maximum value
}
console.log("0 - 1 unchecked == max?", huge == type(uint256).max);
}
}wrapped: 0
0 - 1 unchecked == max? true
Warning: Only use
uncheckedwhen you can prove the math is safe, for examplei++in a loop bounded by an array length. Never wrap balance arithmetic inunchecked.
Choosing a size
- Use
uint256by default. It is the EVM's native word size and the type most standards (like ERC-20) expect. - Use smaller types (
uint64,uint128,uint32) when packing several values into one storage slot inside a struct. The Gas lesson shows the savings. - Use signed integers only when negative values make sense, such as temperature changes or profit/loss.
Test yourself
In Solidity 0.8+, what happens when uint8 x = 255; x += 1; runs outside an unchecked block?
- x becomes 0
- x becomes 256
- The transaction reverts with a panic
- It does not compile
What is -7 / 2 for int256 values?
- -4
- -3
- -3.5
- It reverts
When is an unchecked block appropriate?
- For all balance updates, to save gas
- When you can prove the operation cannot overflow, or want wrap-around on purpose
- Whenever a function is
view - Never; it is deprecated