Integers, my friends, are like the building blocks of numbers in the digital realm. But here’s the exciting part: they come in two distinct flavors - signed and unsigned.
So, what’s the deal with signed integers? Well, they are the versatile number ninjas that can handle both positive and negative values. In Solidity lingo, we define signed integers using the keyword int
, followed by the size of the integer. Now, why size, you ask? Think of it like the container that holds these numbers. The size can be any multiple of 8, ranging from 8 to 256. If you don’t specify a size, it defaults to int256
.
For instance, picture an int8
as a small jar that can snugly fit numbers from -128 to 127. On the other end of the spectrum, an int256
is like a massive vault, capable of accommodating values from -2^255 to 2^255 - 1. Quite a range, isn’t it?
Now, let’s talk about unsigned integers. These are like the eternal optimists of the integer world; they deal only with positive values. In Solidity, we denote them with the keyword uint
, followed by the size, just like their signed counterparts. The size range is the same as signed integers, from 8 to 256.
Imagine a uint16
as a cheerful container, holding values from 0 to 65535. It doesn’t bother with negatives, only focusing on the sunny side of numbers.
Now lets write some Solidity Code.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract SolidityIntegers {
// Unsigned integers are positive numbers only
uint256 myUnsignedInteger = 19;
// You have to note that signed integers can be both positive and negative
int256 mySignedInteger = -19;
}