Java Bit Manipulation Program · OT29
Count Set Bits in an Integer in Java
n & (n - 1) clears the lowest 1 bit. Repeat it until the number is 0, and the number of steps is the number of 1 bits.
Problem Statement
Return how many bits are 1 in the 32-bit binary representation of an int.
Negative numbers use two's complement, so -1 has all 32 bits set.
Input/Output Examples
29Output4Explanation: 29 is 11101 in binary.255Output8Explanation: eight 1 bits.-1Output32Explanation: two's complement of -1 is all ones.Visual Explanation
- n = 11101n & (n-1)11100clears the last 1 count 1
- n = 11100n & (n-1)11000count 2
- n = 11000n & (n-1)10000count 3
- n = 10000n & (n-1)0count 4
- Done4
Subtracting 1 flips the lowest 1 bit to 0 and all the 0 bits below it to 1. AND-ing with the original then clears exactly that one bit.
Approaches
Both approaches handle negative numbers. Kernighan's loop runs once per 1 bit; the shift loop always checks 32 bits.
Approach 1: Kernighan's Algorithm (Recommended)
Idea: repeat n &= n - 1 until n is 0, counting the steps.
- Set
count = 0. - While
n != 0, clear the lowest set bit and increasecount. - Return
count.
Time: O(k) · Space: O(1). k is the number of 1 bits.
Approach 2: Check Every Bit
Idea: add n & 1 32 times, shifting right with >>>.
- Loop 32 times.
- Add the lowest bit.
- Shift right without sign extension.
Time: O(32) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Kernighan's Algorithm (Recommended)
Fast for sparse numbers, where only a few bits are set.
public class SetBitsKernighan {
// n & (n - 1) clears the lowest 1 bit, so the loop runs once per set bit
static int countBits(int n) {
int count = 0;
while (n != 0) {
n &= n - 1;
count++;
}
return count;
}
public static void main(String[] args) {
int[] inputs = {29, 255, 0, -1};
for (int n : inputs) {
System.out.println(n + " -> " + countBits(n));
}
}
}
29 -> 4 255 -> 8 0 -> 0 -1 -> 32
Time complexity: O(k) · Space complexity: O(1).
Approach 2: Check Every Bit
A fixed amount of work, and easy to understand bit by bit.
public class SetBitsShift {
// Checks the lowest bit 32 times, shifting right without the sign (>>>)
static int countBits(int n) {
int count = 0;
for (int i = 0; i < 32; i++) {
count += n & 1;
n >>>= 1;
}
return count;
}
public static void main(String[] args) {
int[] inputs = {29, 255, 0, -1};
for (int n : inputs) {
System.out.println(n + " -> " + countBits(n));
}
}
}
29 -> 4 255 -> 8 0 -> 0 -1 -> 32
Time complexity: O(32) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Kernighan's Algorithm | O(k) | O(1) |
| Check Every Bit | O(32) | O(1) |
Kernighan's loop runs once per set bit, at most 32 times; the shift loop always runs 32 times. Both are constant time for an int.
Interview Tips and Recommended Approach
- Use
>>>, not>>. With>>, a negative number never reaches 0. - Explain why
n & (n - 1)works with a small binary example. - Know
Integer.bitCount(). The JDK uses a branch-free parallel counting trick. - Reuse the trick.
n & (n - 1) == 0also tests for a power of two.
Recommended Approach: Kernighan's Algorithm
Why: it runs only as many times as there are 1 bits and teaches the most useful bit trick in interviews.