Skip to content

Instantly share code, notes, and snippets.

@alexanderattar
Created November 24, 2021 20:22
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 alexanderattar/8002d18dd7153fc2d7d7f6d2356328e0 to your computer and use it in GitHub Desktop.
Save alexanderattar/8002d18dd7153fc2d7d7f6d2356328e0 to your computer and use it in GitHub Desktop.
Shift bytes with Solidity
contract ShiftBytes {
function replaceBytesAtIndex(
bytes32 original,
uint256 position,
bytes1 toInsert
) public pure returns (bytes32) {
bytes1 maskBytes = 0xff;
bytes32 mask = bytes32(maskBytes) >> ((position * 1) * 8);
return (~mask & original) | (bytes32(toInsert) >> ((position * 1) * 8));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment