Laurent Knauss Software Engineer
bitmap illustration

OpenZeppelin bitmaps.sol is an OpenZeppelin solidity library providing a data structure for managing boolean values, designed to store and manipulate large arrays of booleans, thus dramatically reducing gas cost.

Instead of writing bool[] array that occupies 1 storage slot per boolean, we write uint256[] which is 1 storage slot per 256 booleans.

Real-world use cases

  • Tracking user claims for an airdrop (hasAddressClaimed: true/false)
  • Voting systems to track voter participation (hasVoted: true/false)

Storage cost comparison

1. Without BitMaps.sol library:

mapping(address => bool) voted;
// in such case , if we have 20,000 users = 20,000 storage slots.

2. With BitMaps.sol library:

BitMaps.BitMap voted;
// in this scenario, 20,000 users = 20,000/256 = ~78 storage slots (massive gas savings !!)

Example Voting Function

import "@openzeppelin/contracts/utils/struct/BitMaps.sol";

// Use BitMaps for gas optimization
using BitMaps for BitMaps.BitMap;
BitMaps.BitMap private hasVoted;

event VoterStatusUpdated(address indexed voter, bool hasVoted);