Skip to content

Latest commit

 

History

History
23 lines (19 loc) · 415 Bytes

bit-counting.md

File metadata and controls

23 lines (19 loc) · 415 Bytes
title description author tags
Bit Counting
Counts the set bits in the binary representation of an integer
Mcbencrafter
math,number,bits,bit-counting
public static int countBits(int number) {
    int bits = 0;
        
    while (number > 0) {
        bits += number & 1;
        number >>= 1;
    }

    return bits;
}

// Usage:
int number = 5;
System.out.println(countBits(5)); // 2 (101)