2 min read

Integers

Introduction

Integers in Solidity are a primitive data type that represent whole numbers. There are two types of integers in Solidity: signed integers and unsigned integers.

Numeric Data Types in Solidity

First, let's take a quick look at the numeric data types supported in Solidity.

Solidity supports both signed and unsigned integers. However, fixed-point numbers support is not fully implemented in Solidity. The most common integer types are:

  • int8 - a signed 8-bit integer
  • int16 - a signed 16-bit integer
  • int32 - a signed 32-bit integer
  • int64 - a signed 64-bit integer
  • int128 - a signed 128-bit integer
  • int256 - a signed 256-bit integer

For unsigned integers, simply replace int with uint.

Signed Integers

Signed integers can represent both positive and negative values. Signed integers can be declared using the 'int' keyword, followed by the number of bits used to represent the integer value.

For example:

int8 a = -128;
int16 b = 32767;
int256 c = 1234567890;

In this code, the signed integer variables 'a', 'b', and 'c' are declared and initialized with the values -128, 32767, and 1234567890, respectively.

In this code, the signed integer variables 'a', 'b', and 'c' are declared and initialized with the values -128, 32767, and 1234567890, respectively.

Integer Overflow and Underflow

Integer overflow and underflow occur when an integer value exceeds its maximum or minimum value. For signed integers, the maximum value is 2^(n-1)-1 and the minimum value is -2^(n-1), where n is the number of bits used to represent the integer value. For unsigned integers, the maximum value is 2^n-1 and the minimum value is 0.

Integer overflow and underflow can cause unexpected behavior in smart contracts, and can potentially lead to security vulnerabilities. To prevent integer overflow and underflow, it is important to carefully choose the data types used in smart contracts, and to implement proper error handling and input validation.

Smart Contract Example

Let's see an example within the following Sample Smart contract written in Solidity 0.8.16.

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

contract SimpleMath {
    int256 public num1;
    int256 public num2;

    constructor(int256 _num1, uint256 _num2) {
        num1 = _num1;
        num2 = _num2;
    }

    function add() public view returns (int256) {
        return num1 + int256(num2);
    }

    function subtract() public view returns (int256) {
        return num1 - int256(num2);
    }

    function multiply() public view returns (int256) {
        return num1 * int256(num2);
    }

    function divide() public view returns (int256) {
        require(num2 != 0, "division by zero");
        return num1 / int256(num2);
    }

    function modulus() public view returns (int256) {
        require(num2 != 0, "division by zero");
        return num1 % int256(num2);
    }

    function increment() public {
        num1++;
    }

    function decrement() public {
        num1--;
    }
}

In this code, the 'SimpleMath' contract declares two integer variables: num1 and num2 of type 'int256'. The constructor initializes these variables with the values passed in as arguments.

The contract contains several functions that perform mathematical operations on the integer variables. There are 'add', 'subtract', 'multiply', 'divide', and 'modulus' functions, which use the standard arithmetic operation on variable num1 and num2. The require function is used to implement input validation and prevent division by zero in the 'divide' and 'modulus' functions.

The 'increment' and 'decrement' methods use the increment and decrement operators to modify the value of num1.

Conclusion

In conclusion, integers are an important aspect of Solidity programming, and can be used to represent whole numbers in smart contracts. Solidity provides several built-in integer methods and operators, which can be used to perform mathematical and bitwise operations on integer variables. Understanding how to work with integers in Solidity, including integer overflow and underflow, is important for writing safe and efficient smart contracts.

Feedback

Have feedback? Found something that needs to be updated, accurate, or explained?

Join our discord and share what can be improved.

Any and all feedback is welcomed.