Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a BitMap struct #2710

Merged
merged 10 commits into from
Jun 11, 2021
Prev Previous commit
Next Next commit
cleanup BitMaps code
  • Loading branch information
Amxx committed Jun 10, 2021
commit 22ea07c5570410b10a2decbd6c6ffe7e4d2cc18a
14 changes: 6 additions & 8 deletions contracts/utils/structs/BitMaps.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ library BitMaps {

function get(BitMap storage bitmap, uint256 index) internal view returns (bool) {
Amxx marked this conversation as resolved.
Show resolved Hide resolved
uint256 bucket = index / 256;
uint256 pos = index % 256;
uint256 word = bitmap._data[bucket];
uint256 mask = (1 << pos);
return word & mask != 0;
uint256 mask = 1 << (index % 256);
return bitmap._data[bucket] & mask != 0;
}

function set(BitMap storage bitmap, uint256 index) internal {
Amxx marked this conversation as resolved.
Show resolved Hide resolved
uint256 bucket = index / 256;
uint256 pos = index % 256;
bitmap._data[bucket] |= (1 << pos);
uint256 mask = 1 << (index % 256);
bitmap._data[bucket] |= mask;
}

function unset(BitMap storage bitmap, uint256 index) internal {
Amxx marked this conversation as resolved.
Show resolved Hide resolved
uint256 bucket = index / 256;
uint256 pos = index % 256;
bitmap._data[bucket] &= ~(1 << pos);
uint256 mask = 1 << (index % 256);
bitmap._data[bucket] &= ~mask;
}
}