In Java, Integer.bitCount(i) returns the number of 1 bits in the 32-bit binary form of i. This count is often called the population count, or popcount.
For example, 7 is 111 in binary, so its bit count is 3, and -1 has all 32 bits set, so its bit count is 32. The method uses a fast built-in routine, so you never need to loop over the bits yourself.
A static method of java.lang.Integer (since Java 1.5): call it on the class, as Integer.bitCount(...). Integer lives in java.lang, so no import is needed.
Syntax
One argument: the int to examine.
Java
int a = Integer.bitCount(7); // 3 (111)
int b = Integer.bitCount(8); // 1 (1000)
int c = Integer.bitCount(-1); // 32 (all bits set)
Call
What it does
Integer.bitCount(i)
The number of 1 bits in i.
Integer.bitCount(n) == 1
true when n is a power of two (and above zero).
Integer.bitCount(a ^ b)
How many bits differ between a and b.
Integer.toBinaryString(i)
Shows the bits that are being counted.
Negative numbers count all 32 bits of their two's complement form, so the result for a negative number is often large.
Library declarations
Java
// java.lang.Integer
public static int bitCount(int i)
Parameters
Parameter
Description
i (int)
The number whose set bits you want to count.
Return Value
Return Value
Description
int
The number of 1 bits, from 0 (for the number 0) to 32 (for -1).
Print each number in binary next to its bit count.
Java
public class IntegerBitCountDemo {
public static void main(String[] args) {
int[] numbers = {0, 1, 7, 8, 255, -1};
for (int n : numbers) {
System.out.println(n + " -> " + Integer.bitCount(n)
+ " bits set (" + Integer.toBinaryString(n) + ")");
}
}
}
Output
0 -> 0 bits set (0)
1 -> 1 bits set (1)
7 -> 3 bits set (111)
8 -> 1 bits set (1000)
255 -> 8 bits set (11111111)
-1 -> 32 bits set (11111111111111111111111111111111)
How It Works
Zero has none. The number 0 has no 1 bits at all, so the count is 0.
Powers of two have one. 1 is 1 and 8 is 1000, so both count 1.
All ones. 7 is 111 (3 bits) and 255 is 11111111 (8 bits).
Negative numbers. -1 has every one of its 32 bits set, so the count is 32.
Example 2 — Test for Powers of Two
Use bitCount(n) == 1 to find out whether a number is a power of two.
Java
public class IntegerBitCountPower {
static boolean isPowerOfTwo(int n) {
return n > 0 && Integer.bitCount(n) == 1;
}
public static void main(String[] args) {
int[] numbers = {1, 2, 6, 16, 100, 1024, 0, -8};
for (int n : numbers) {
System.out.println(n + " is a power of two: " + isPowerOfTwo(n));
}
}
}
Output
1 is a power of two: true
2 is a power of two: true
6 is a power of two: false
16 is a power of two: true
100 is a power of two: false
1024 is a power of two: true
0 is a power of two: false
-8 is a power of two: false
How It Works
One bit means a power of two. Every power of two is a single 1 followed by zeros in binary.
True cases. 1, 2, 16, and 1024 each have exactly one set bit.
False cases. 6 (110) has two bits and 100 (1100100) has three, so they are not powers of two.
Guard against 0 and negatives. Zero has no bits, and -8 has many, so the extra n > 0 check keeps the answer correct.
Example 3 — Measure How Many Bits Differ
XOR two numbers to mark the differing bits, then count them with bitCount.
Java
public class IntegerBitCountDistance {
static int distance(int a, int b) {
return Integer.bitCount(a ^ b);
}
public static void main(String[] args) {
System.out.println("5 vs 9: " + distance(5, 9));
System.out.println("255 vs 0: " + distance(255, 0));
System.out.println("1023 vs 1024: " + distance(1023, 1024));
System.out.println("42 vs 42: " + distance(42, 42));
System.out.println("binary 5 ^ 9: " + Integer.toBinaryString(5 ^ 9));
}
}
Output
5 vs 9: 2
255 vs 0: 8
1023 vs 1024: 11
42 vs 42: 0
binary 5 ^ 9: 1100
How It Works
XOR marks differences. The ^ operator gives a 1 in every position where the two numbers have different bits.
5 and 9. 0101 and 1001 differ in two places, and the XOR is 1100, which has 2 set bits.
Extreme cases. 255 and 0 differ in all 8 low bits, and 1023 and 1024 differ in 11 bits.
Equal numbers. 42 and 42 have no differences, so the distance is 0.
Frequently Asked Questions
It returns how many bits are 1 in the 32-bit binary form of an int. For example, Integer.bitCount(7) is 3.
Use n > 0 && Integer.bitCount(n) == 1. A power of two has exactly one set bit.
It counts the set bits in the two's complement form, so -1 gives 32 and -8 gives 29.
bitCount counts 1 bits, not digits. Integer.toBinaryString(n).length() gives the number of binary digits.
XOR them and count the result: Integer.bitCount(a ^ b) is the number of bit positions where they differ.