Articles → BLOCKCHAIN → Integer Rollover In Solidity

Integer Rollover In Solidity






How Does Integer Rollover Happen?









Unchecked Keyword In Solidity





Example




// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

contract ExampleIntegerRollOver {

    uint8 public myUint8Decrement = 0; 
    uint8 public myUint8Increment = 255;

    function decrementUintUnchecked() public {
        unchecked {
            myUint8Decrement--;
        }
    }

    function decrementUint() public {
        myUint8Increment++;
    }

    function incrementUintUnchecked() public {
        unchecked {
            myUint8Increment++;
        }
    }

    function incrementUint() public {
        myUint8Decrement--;
    }

}





Decrementuintunchecked Function




Picture showing the initial value of the myUint8Decrement variable




Picture showing the value of myUint8Decrement variable when decrementUintUnchecked is clicked





Decrementuint Function





Incrementuintunchecked Function




Picture showing the value of myUint8Increment variable when IncrementUintUnchecked is clicked



Incrementuint Function





Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, October 15, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250