Hacker News new | ask | show | jobs
by Jabbles 4876 days ago

    int res = (*fn[(n < 2)])(n);
As stated by others, "n < 2" is a conditional or comparison or whatever (let's just say it's cheating :P) let's call it "m".

So we choose our function pointer with:

    int res = (*fn[m])(n);
where

    uint32_t m = n < 2?1:0;
and let's assume n is non-negative...

    uint32_t top_bits = n & 0xFFFFFFFEu;
    uint32_t p = 0;
    for (int i = 0; i < 32; i++){
        p |= (top_bits & (1u << i)) >> i;
    }
    uint32_t m = 1 - p;
Edit:

You can do the for loop to check if any bits are set in logarithmic time. For general comparisons see http://stackoverflow.com/questions/10096599/bitwise-operatio...

Oh and the "i < 32" doesn't count as you could just unroll it...