Skip to content

Instantly share code, notes, and snippets.

@ikwuoz
Last active October 10, 2022 10:29
Show Gist options
  • Select an option

  • Save ikwuoz/b50381d110a47f1e4b6f8a3955f1adfe to your computer and use it in GitHub Desktop.

Select an option

Save ikwuoz/b50381d110a47f1e4b6f8a3955f1adfe to your computer and use it in GitHub Desktop.
hex bit slicing for smol gas saving
// Execution Cost: ~25200
// Logic is to divide the packedbits using a hex denomitor that effectively slices out byte sections from it
// ex: In decimal (1024 / 10^2 = 10), since evm works with 32bytes hex, we use a power of 16 here i.e (result / 16^n)
// for hex division slicing
uint constant eightBytesSlice = 0x10000000000000000;
uint constant sixteenBytesSlice = 0x100000000000000000000000000000000;
uint constant twentyfourBytesSlice = 0x1000000000000000000000000000000000000000000000000;
function gradeStudentResultBigMagicSlice(address _student) external view returns(uint _datda, uint _potions, uint _transfigurations, uint _comc) {
uint256 resultBitMagic = bitMagicResults[_student];
_datda = resultBitMagic & SINGLE_SUBJECT_MASK;
_potions = (resultBitMagic / eightBytesSlice) & SINGLE_SUBJECT_MASK;
_transfigurations = (resultBitMagic / sixteenBytesSlice) & SINGLE_SUBJECT_MASK;
_comc = (resultBitMagic / twentyfourBytesSlice);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment