Created
November 24, 2021 20:22
-
-
Save alexanderattar/8002d18dd7153fc2d7d7f6d2356328e0 to your computer and use it in GitHub Desktop.
Shift bytes with Solidity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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