Skip to content

Instantly share code, notes, and snippets.

@lcfr-eth
Created January 30, 2023 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcfr-eth/12f18de4543c2a133ac06186f4691676 to your computer and use it in GitHub Desktop.
Save lcfr-eth/12f18de4543c2a133ac06186f4691676 to your computer and use it in GitHub Desktop.
pragma solidity ^0.8.0;
contract AlignmentChecker {
function checkAlignment(uint256 x) public view returns (uint256) {
assembly {
let alignment := 32
let num := x
let mask := and(num, sub(alignment, 1))
let isAligned := iszero(mask)
if isAligned {
// number is aligned
return (0x00, 32)
}
let remainder := sub(alignment, mask)
mstore(0x00, remainder)
return (0x00, 32)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment