Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
4.9 KiB
LibBit
Library for bit twiddling and boolean operations.
Bit Twiddling Operations
fls(uint256)
function fls(uint256 x) internal pure returns (uint256 r)
Find last set.
Returns the index of the most significant bit of x,
counting from the least significant bit position.
If x is zero, returns 256.
clz(uint256)
function clz(uint256 x) internal pure returns (uint256 r)
Count leading zeros.
Returns the number of zeros preceding the most significant one bit.
If x is zero, returns 256.
ffs(uint256)
function ffs(uint256 x) internal pure returns (uint256 r)
Find first set.
Returns the index of the least significant bit of x,
counting from the least significant bit position.
If x is zero, returns 256.
Equivalent to ctz (count trailing zeros), which gives
the number of zeros following the least significant one bit.
popCount(uint256)
function popCount(uint256 x) internal pure returns (uint256 c)
Returns the number of set bits in x.
countZeroBytes(uint256)
function countZeroBytes(uint256 x) internal pure returns (uint256 c)
Returns the number of zero bytes in x.
To get the number of non-zero bytes, simply do 32 - countZeroBytes(x).
countZeroBytes(bytes)
function countZeroBytes(bytes memory s) internal pure returns (uint256 c)
Returns the number of zero bytes in s.
To get the number of non-zero bytes, simply do s.length - countZeroBytes(s).
countZeroBytesCalldata(bytes)
function countZeroBytesCalldata(bytes calldata s)
internal
pure
returns (uint256 c)
Returns the number of zero bytes in s.
To get the number of non-zero bytes, simply do s.length - countZeroBytes(s).
isPo2(uint256)
function isPo2(uint256 x) internal pure returns (bool result)
Returns whether x is a power of 2.
reverseBits(uint256)
function reverseBits(uint256 x) internal pure returns (uint256 r)
Returns x reversed at the bit level.
reverseBytes(uint256)
function reverseBytes(uint256 x) internal pure returns (uint256 r)
Returns x reversed at the byte level.
commonBitPrefix(uint256,uint256)
function commonBitPrefix(uint256 x, uint256 y)
internal
pure
returns (uint256)
Returns the common prefix of x and y at the bit level.
commonNibblePrefix(uint256,uint256)
function commonNibblePrefix(uint256 x, uint256 y)
internal
pure
returns (uint256)
Returns the common prefix of x and y at the nibble level.
commonBytePrefix(uint256,uint256)
function commonBytePrefix(uint256 x, uint256 y)
internal
pure
returns (uint256)
Returns the common prefix of x and y at the byte level.
toNibbles(bytes)
function toNibbles(bytes memory s)
internal
pure
returns (bytes memory result)
hex"ABCD" -> hex"0A0B0C0D".
Boolean Operations
A Solidity bool on the stack or memory is represented as a 256-bit word.
Non-zero values are true, zero is false.
A clean bool is either 0 (false) or 1 (true) under the hood.
Usually, if not always, the bool result of a regular Solidity expression,
or the argument of a public/external function will be a clean bool.
You can usually use the raw variants for more performance.
If uncertain, test (best with exact compiler settings).
Or use the non-raw variants (compiler can sometimes optimize out the double iszeros).
rawAnd(bool,bool)
function rawAnd(bool x, bool y) internal pure returns (bool z)
Returns x & y. Inputs must be clean.
and(bool,bool)
function and(bool x, bool y) internal pure returns (bool z)
Returns x & y.
and(bool,bool,bool)
function and(bool w, bool x, bool y) internal pure returns (bool z)
Returns w & x & y.
and(bool,bool,bool,bool)
function and(bool v, bool w, bool x, bool y)
internal
pure
returns (bool z)
Returns v & w & x & y.
rawOr(bool,bool)
function rawOr(bool x, bool y) internal pure returns (bool z)
Returns x | y. Inputs must be clean.
or(bool,bool)
function or(bool x, bool y) internal pure returns (bool z)
Returns x | y.
or(bool,bool,bool)
function or(bool w, bool x, bool y) internal pure returns (bool z)
Returns w | x | y.
or(bool,bool,bool,bool)
function or(bool v, bool w, bool x, bool y)
internal
pure
returns (bool z)
Returns v | w | x | y.
rawToUint(bool)
function rawToUint(bool b) internal pure returns (uint256 z)
Returns 1 if b is true, else 0. Input must be clean.
toUint(bool)
function toUint(bool b) internal pure returns (uint256 z)
Returns 1 if b is true, else 0.