Skip to content

Instantly share code, notes, and snippets.

@IllIllI000
Created July 9, 2022 19:17
Show Gist options
  • Save IllIllI000/9388d20c70f9a4632eb3ca7836f54977 to your computer and use it in GitHub Desktop.
Save IllIllI000/9388d20c70f9a4632eb3ca7836f54977 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0; // optimize 200
/**
* @title DowncastUseVsUseDowncast
* @author IllIllI
*/
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