Search code examples
c++11time-complexitybitwise-operatorsstd-bitset

c++ bitset logical operations in O(log n)?


According to this post Performance of doing bitwise operations on bitsets performance is O(n) how do I make it O(log n).


Solution

  • The answer is you don't.

    Assume you have a bitset of n size. Let's look at the xor operator ^. It obviously has to look at each bit in both operands, so it makes 2n lookups. This results in a complexity of O(n).

    You can use assembler instructions that e.g. do it for 32 bits at a time, so the number of operations is (n+31)/32, but this doesn't change that the complexity is O(n). After all complexity is calculated for n towards infinity and constant factors are disregarded.