Primitive Values & Types

What are we working with?

Although this course is primarily focused on developing smart contracts, it is crucial to understand the values that we will be working with; after all, code is just a collection of expressions that all add up to something meaningful.

Being a statically-typed programming language, Solidity requires us to declare the types of each value we utilize in our smart contracts. This section will therefore focus on the primitive types that Solidity offers.

Primer on Integer Sizes

Before discussing any types, it is important to discuss the concept of integer sizes (or to be mathematically correct, the magnitude of integers)

Languages like Python make it seem as if we are able to store arbitrarily large integers with no limitations. However, developers who use languages such as Rust or C will know that this is not the case. Integer types in most programming languages are restricted to a certain range; this is due to the fact that we are unable to store infinitely large integers (as this would imply that we have infinitely large memory).

What defines the range of integer types? Recall that all integers can be written in binary form. Listed below are integers (represented in decimal format) and their associated binary representation:

19 => 10011
432 => 110110000
5 => 101

The binary representation of the integers above consist of bits (i.e. 0s or 1s). It is bits that are used to define the range of integer types. As an example, consider a 32-bit integer; the 32-bit part refers the fact that the integer can only store integers whose binary representation consists of at most 32 bits.

From a mathematical perspective, a n-bit integer means that any integer x within the range 0 <= n <= 2**n - 1 can be stored.

What About Signed Integers?

Signed integers are represented in binary via the two's complement formula. Although we won't get into how to compute the binary representation of a signed integer, note that a n-bit signed integer means that any integer x within the range -2**(n-1) <= x <= 2**(n-1) - 1 can be stored

Integers (Signed and Unsigned)

In Solidity, the two general integer types available to us are uint (unsigned integer) and int (signed integer). Both uint and int are of size 256-bits. However, these are not the only integer types that we are restricted to; in fact, we can utilize any signed/unsigned integer type of size n where n is a multiple of 8. The following is a list of valid integer types in Solidity:

uint8, uint16, uint24, uint32, ..., uint248, uint256
int8, int16, int24, int32, ..., int248, int256

Integer Operations

Assuming a, b are integers and c is a non-zero integer, listed below are the operations that we can perform on integers in Solidity

a + b; // Addition
a - b; // Subtraction
a * b; // Multiplication
a / c; // Division
a % c; // Modulo
a ** b; // Exponentiation

Booleans

Solidity provides us with the boolean values true and false.

Address

Since addresses are commonly used within smart contracts, Solidity provides us with an address type. An example of the address type is as follows:

address myAddress = 0x3D2A511EbB0f53337749B4bd758feF2658f7E69e;

Last updated