/** * Copyright 2017–2019, LaborX PTY * Licensed under the AGPL Version 3 license. */ pragma solidity ^0.4.25; library Bits { uint constant internal ONE = uint(1); uint constant internal ONES = uint(~0); // Sets the bit at the given 'index' in 'self' to '1'. // Returns the modified value. function setBit(uint self, uint8 index) internal pure returns (uint) { return self | ONE << index; } // Sets the bit at the given 'index' in 'self' to '0'. // Returns the modified value. function clearBit(uint self, uint8 index) internal pure returns (uint) { return self & ~(ONE << index); } // Sets the bit at the given 'index' in 'self' to: // '1' - if the bit is '0' // '0' - if the bit is '1' // Returns the modified value. function toggleBit(uint self, uint8 index) internal pure returns (uint) { return self ^ ONE << index; } // Get the value of the bit at the given 'index' in 'self'. function bit(uint self, uint8 index) internal pure returns (uint8) { return uint8(self >> index & 1); } // Check if the bit at the given 'index' in 'self' is set. // Returns: // 'true' - if the value of the bit is '1' // 'false' - if the value of the bit is '0' function bitSet(uint self, uint8 index) internal pure returns (bool) { return self >> index & 1 == 1; } // Checks if the bit at the given 'index' in 'self' is equal to the corresponding // bit in 'other'. // Returns: // 'true' - if both bits are '0' or both bits are '1' // 'false' - otherwise function bitEqual(uint self, uint other, uint8 index) internal pure returns (bool) { return (self ^ other) >> index & 1 == 0; } // Get the bitwise NOT of the bit at the given 'index' in 'self'. function bitNot(uint self, uint8 index) internal pure returns (uint8) { return uint8(1 - (self >> index & 1)); } // Computes the bitwise AND of the bit at the given 'index' in 'self', and the // corresponding bit in 'other', and returns the value. function bitAnd(uint self, uint other, uint8 index) internal pure returns (uint8) { return uint8((self & other) >> index & 1); } // Computes the bitwise OR of the bit at the given 'index' in 'self', and the // corresponding bit in 'other', and returns the value. function bitOr(uint self, uint other, uint8 index) internal pure returns (uint8) { return uint8((self | other) >> index & 1); } // Computes the bitwise XOR of the bit at the given 'index' in 'self', and the // corresponding bit in 'other', and returns the value. function bitXor(uint self, uint other, uint8 index) internal pure returns (uint8) { return uint8((self ^ other) >> index & 1); } // Gets 'numBits' consecutive bits from 'self', starting from the bit at 'startIndex'. // Returns the bits as a 'uint'. // Requires that: // - '0 < numBits <= 256' // - 'startIndex < 256' // - 'numBits + startIndex <= 256' function bits(uint self, uint8 startIndex, uint16 numBits) internal pure returns (uint) { require(0 < numBits && startIndex < 256 && startIndex + numBits <= 256); return self >> startIndex & ONES >> 256 - numBits; } // Computes the index of the highest bit set in 'self'. // Returns the highest bit set as an 'uint8'. // Requires that 'self != 0'. function highestBitSet(uint self) internal pure returns (uint8 highest) { require(self != 0); uint val = self; for (uint8 i = 128; i >= 1; i >>= 1) { if (val & (ONE << i) - 1 << i != 0) { highest += i; val >>= i; } } } // Computes the index of the lowest bit set in 'self'. // Returns the lowest bit set as an 'uint8'. // Requires that 'self != 0'. function lowestBitSet(uint self) internal pure returns (uint8 lowest) { require(self != 0); uint val = self; for (uint8 i = 128; i >= 1; i >>= 1) { if (val & (ONE << i) - 1 == 0) { lowest += i; val >>= i; } } } }