Take: #define n 8 #define N ((1u<<n)-1) unsigned countLeadingZeros32(unsigned x) { if (x > N) __builtin_unreachable(); return (N - x) ^ N; } This should be optimized to just return x; Like it is done by LLVM.