Skip to content

Latest commit

 

History

History
20 lines (17 loc) · 315 Bytes

highest_bit.md

File metadata and controls

20 lines (17 loc) · 315 Bytes

Bit operations: highest set bit

int highBit(unsigned int n) {
    n |= n >> 1;
    n |= n >> 2;
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
    return 31 - __builtin_clz((n + 1) >> 1);
}
31 - bits.LeadingZeros32(uint32(n))
31 - Integer.numberOfLeadingZeros(k - 1);