Search code examples
c++mathboolean-operations

Performance of operator| vs operators+


Is there any major difference between | and + that would affect a code's performance in the long run? or are both O(1)? the code i am working with is something like this:

uint64_t dostuff(uint64_t a,uint64_t b){
        // the max values of the inputs are 2^32 - 1

        // lots of stuff involving boolean operators
        // that have no way of being substituted by 
        // arithmetic operators

        return (a << 32) + b;
        //or
        return (a << 32) | b;
}

the code will be used many times, so i want to speed it up as much as possible.


Solution

  • No performance difference on any modern computer.

    The two operators have different meaning though. If the bit is already set, | will do nothing, but + will clear the bit and all the following non-zero bits and set the next zero bit to 1.