Java Integer bitCount() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

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)
CallWhat it does
Integer.bitCount(i)The number of 1 bits in i.
Integer.bitCount(n) == 1true 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

ParameterDescription
i (int)The number whose set bits you want to count.

Return Value

Return ValueDescription
intThe number of 1 bits, from 0 (for the number 0) to 32 (for -1).

Compare with Similar Methods

Related bit methods:

MethodHow it differs
toBinaryString(i)Shows the bits themselves as text.
highestOneBit(i)Finds the leftmost 1 bit instead of counting them.
numberOfTrailingZeros(i)Counts zeros after the last 1 bit.
lowestOneBit(i)Isolates the rightmost 1 bit.

Java Examples

Example 1 — Count the Set Bits of Several Numbers

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) + ")");
        }
    }
}

How It Works

  1. Zero has none. The number 0 has no 1 bits at all, so the count is 0.
  2. Powers of two have one. 1 is 1 and 8 is 1000, so both count 1.
  3. All ones. 7 is 111 (3 bits) and 255 is 11111111 (8 bits).
  4. 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));
        }
    }
}

How It Works

  1. One bit means a power of two. Every power of two is a single 1 followed by zeros in binary.
  2. True cases. 1, 2, 16, and 1024 each have exactly one set bit.
  3. False cases. 6 (110) has two bits and 100 (1100100) has three, so they are not powers of two.
  4. 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));
    }
}

How It Works

  1. XOR marks differences. The ^ operator gives a 1 in every position where the two numbers have different bits.
  2. 5 and 9. 0101 and 1001 differ in two places, and the XOR is 1100, which has 2 set bits.
  3. Extreme cases. 255 and 0 differ in all 8 low bits, and 1023 and 1024 differ in 11 bits.
  4. 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.

Next: byteValue()

Narrow an Integer to a primitive byte.

byteValue() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful