Most optimal C/C++ solution on modern processors


  • 0
    H

    The most optimal solution on modern processors would use only one hardware instruction (popcnt). The following works on GCC/G++, and resolves to an optimized bitwise version if a hardware popcnt is not supported:

    int hammingWeight(uint32_t n) {
        return __builtin_popcount(n);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.