2 min read

Unsigned Integers

Introduction

Unsigned integers in Solidity are a primitive data type that represent positive whole numbers. Unlike signed integers, which can represent both positive and negative values, unsigned integers can only represent positive values. Unsigned integers can be declared using the 'uint' keyword, followed by the number of bits used to represent the integer value.

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:

  • uint8 - a signed 8-bit integer
  • uint16 - a signed 16-bit integer
  • uint32 - a signed 32-bit integer
  • uint64 - a signed 64-bit integer
  • uint128 - a signed 128-bit integer
  • uint256 - a signed 256-bit integer

For signed integers, simply replace uint with int.

Unsigned Integers

Unsigned integers can only represent positive values. Unsigned integers can be declared using the 'uint' keyword, followed by the number of bits used to represent the integer value. We will cover these next.

For example:

uint8 a = 255;
uint16 b = 65535;
uint256 c = 1234567890;

In this code, the unsigned integer variables 'a', 'b', and 'c' are declared and initialized with the values 255, 65535, and 1234567890, respectively.

Arithmetic Operators

Solidity provides several arithmetic operators that can be used to perform mathematical operations on unsigned integers. The most common arithmetic operators are:

    • (addition): Adds two unsigned integers.
    • (subtraction): Subtracts one unsigned integer from another.
    • (multiplication): Multiplies two unsigned integers.
  • / (division): Divides one unsigned integer by another.
  • % (modulus): Computes the remainder of integer division.
  • ** (exponentiation): Raises one unsigned integer to the power of another.

For example:

uint256 a = 5;
uint256 b = 10;
uint256 c = 100;

uint256 sum = a + b; // 15
uint256 difference = b - a; // 5
uint256 product = a * b; // 50
uint256 quotient = b / a; // 2
uint256 remainder = c % a; // 0
uint256 power = a ** 2; // 25

This code shows an example of arithmetic operators applied to the same values.

Conclusion

In conclusion, unsigned integers are an important aspect of Solidity programming, and can be used to represent positive whole numbers in smart contracts. Solidity provides several built-in methods and arithmetic operators that can be used to manipulate unsigned integers. Understanding how to work with unsigned integers in Solidity, including the built-in methods and arithmetic operators, is important for writing safe and efficient smart contracts.

Overall, unsigned integers are a powerful tool for developing Solidity smart contracts, and can be used to represent a wide range of values and perform a variety of mathematical operations. By understanding how to work with unsigned integers in Solidity, developers can create smart contracts that are efficient, reliable, and secure.

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.