Skip to content

Instantly share code, notes, and snippets.

@0xxfu
Created June 16, 2023 06:56
Show Gist options
  • Save 0xxfu/3672fec07eb3031cd5da14ac015e04a1 to your computer and use it in GitHub Desktop.
Save 0xxfu/3672fec07eb3031cd5da14ac015e04a1 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0; // optimize 200
/**
* @title Smaller Uint int
* @author 0xxfu
*/
contract DowncastUseVsUseDowncast {
function test() external view returns (uint8) {
uint8 varr = uint8(block.number >> 255);
varr = varr + 1; // 21359 gas
//varr = varr + uint8(block.number); // 21461 (adds 102 gas)
//varr = varr + 2; // 21564 (adds 103 gas)
//varr = varr + uint8(block.timestamp); // 21666 (adds 102 gas)
return varr;
}
function test2() external view returns (uint8) {
uint256 varr = block.number >> 255;
varr = varr + 1; // 21331 gas
//varr = varr + block.number; // 21405 (adds 74 gas)
//varr = varr + 2; // 21480 (adds 75 gas)
//varr = varr + block.timestamp; // 21554 (adds 74 gas)
return uint8(varr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment