CS4998: Blockchain Development Textbook
  • CS4998: Blockchain Development
  • Prerequisites
  • Introduction
    • Blockchain Theory
      • Bitcoin and the UTXO Model
      • Ethereum and the State-Based Model
    • Remix - A First Glance
    • Hello World!
      • Solidity File Structure
      • Primitive Values & Types
      • Contract Structure
      • Functions
      • Data Structures
      • Summary & Exercises
    • Hello World! Pt. 2
      • Control Flow
      • Interfaces and Inheritance
      • Constructors
      • Contract Interactions
      • Modifiers
      • Dynamic Arrays and Strings
        • Dynamic Arrays
        • Strings
      • Errors
      • Events
      • Units and Global Variables
      • Default Functions
  • Local Development
    • Node Providers
    • Interacting With On-Chain Contracts
    • Migrating to Foundry & VS Code
      • The Basics of Forge
      • Installing and Using Dependencies
      • Cast
      • Anvil
  • Understanding the EVM
    • The Ethereum Virtual Machine
      • A First Look at Computers
      • The Turing Machine
      • EVM Data Structures
      • Operation Codes (Opcodes)
      • Gas
      • Contract Compilation
      • Contract Runtime
    • Gas Optimizations
  • Yul & Advanced EVM Topics
    • Yul
    • Metamorphism
    • Bitwise Manipulations
  • Correctness
    • Security
    • Types of Testing
  • ERC Standards
    • Why ERCs?
    • ERC20
    • ERC721
    • ERC777
    • ERC1155
  • Frequently Used Smart Contracts
    • OpenZeppelin
    • Uniswap
    • Multisignature Contracts
    • AAVE/Compound
  • MEV & Advanced Blockchain Theory
    • Consensus Mechanisms vs Sybil Resistance Mechanisms
    • Maximal Extractable Value (MEV)
    • Looking Past The EVM
  • Etcetera
    • Developer Practices
    • Spring 2023 Past Resources
Powered by GitBook
On this page
  • Primer on Integer Sizes
  • Integers (Signed and Unsigned)
  • Booleans
  • Address
  1. Introduction
  2. Hello World!

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;
PreviousSolidity File StructureNextContract Structure

Last updated 1 year ago