Solidity has two data types for integers: signed and unsigned. Signed integers can store positive or negative values, while unsigned integers can only store positive values. Both types can range from 8 bits up to 256 bits.

A signed integer is declared with the int keyword, an alias for int256, and takes up 32 bytes by default. To save on space, we can specify bit size in steps of 8. For example, int8, int16, int24, all the way to int256.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract Ints {
    int a = 13;
    int256 b = -3516812;
    int8 c = 72;
    int8 d = 322; // 🚫 exceeds limit of uint8
}

An unsigned integer is declared with the uint keyword. Like signed integers, uint is an alias for uint256 and can also use less space by specifying bit size in steps of 8 (uint8, uint16, …, uint256).

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract UInts {
    uint a = 13;
    uint256 b = -3516812; // 🚫 uints can't be negative
    uint8 c = 72;
    uint8 d = 322; // 🚫 exceeds limit of uint8
}

When deciding which integer type to use, it’s important to consider the trade-off between saving gas and accuracy. Smaller integer types can save gas, but they may cause errors if the value you are attempting to store exceeds the type’s limits.