What happens if you overflow or underflow integer in solidity?

Search for a command to run...

No comments yet. Be the first to comment.
In Go, variables are passed by value, which means that when we pass variables as arguments to functions, Go makes a copy of those values for the function to use. However, the behaviour of Slice can be a bit confusing at first glance, especially if we...

As someone who's been using ChatGPT for a while now, I've been exploring how to maximise its benefits and integrate it into my daily life. I've been working on a side project to develop a Discord bot that interacts with ChatGPT, similar to the one pr...

The Go net/http package provides a robust set of functionality to allow us to deal with HTTP requests and responses. However, reading the response body is a common task that requires a bit of caution. In this blog, we will explore the pitfall I exper...

Today we are going to write our own custom json.Unmarshal() method. I find it very useful and can make our code more maintainable and readable if we can make good use of type alias and the json.Unmarshal method. Let's say we want to decode a JSON-enc...

Have you ever tried to maintain services between environments and got lost in all the variables? Or even broken things by accident? Today I'm going to give you some tips on how to speed up your workflow and make things easier. My focus will be mainly...

Solidity is a programming language used to write smart contracts that are run on Ethereum Blockchain Node. To be precise, it runs on Ethereum Virtual Machine(EVM). Today, I’m about to share one of the more interesting behaviours I found in Solidity.
Unsigned Integers
Signed integral types (int) can represent both positive numbers and negative numbers, whereas Unsigned ones (uint) can only represent positive integers
Size of Integers
The default size of integers is 32 bytes, which can be declared as uint. Also, we can explicitly specify the size of integers by adding the trailing number of bits such as uint8 which can store in 8 bits allowing 256 combinations (0 through 255).
What if we try to store the result of some arithmetic that falls outside of the declared range? In other programming languages, like Golang, the software may panic and disable the action ( causing an exception in the program), but it's not the case in Solidity(before version 0.8). It will still be runnable without failing the action, but the result won’t be what we expected, it will roll over to the next digit it can represent or store.
Feel free to try the code snippet below and see if you can overflow or underflow integers.
pragma solidity >=0.6.0 <0.7.0;
contract Overflow {
// value range : 0 ~ 255
uint8 public balance;
function maxTheBalance() public {
balance = 2 ** 8 - 1;
}
// if the balance was 0, after decreasing it by 1, the balance will become 255
function decrease() public {
balance = balance - 1;
}
// if the balance was 255, after increasing it by 1, the balance will become 0
function increase() public {
balance = balance + 1;
}
}