Integer.bitCount() method in Java with Examples

The bitCount() is a static method of the Integer class that counts the number of 1 bit in the two’s complement binary representation of the integer. It was introduced in Java 1.5, and this post will discuss the bitCount() method in detail.

  • Method declaration – public static int bitCount(int i)
  • What does it do? It will count the number of 1’s in the two’s complement binary representation of the number passed in the argument
  • What does it return? It will return an int value denoting the number of 1’s in the two’s complement binary representation

What is the two’s complement of a number? We can easily count the number of 1 bit for a positive number, but what about negative numbers? How to calculate the bit count for negative numbers? For that, we first have to understand the 2’s complement.

This StackOverflow answer has explained 2’s complement quite well. We recommend reading it to get the basic idea of 2’s complement.

But here is a small conclusion on how we can calculate the 2’s complement of a number.

  • Let’s say we have to calculate the 2’s complement of -6.
  • First, write the binary representation of its corresponding positive integer +6.
  • The binary representation of 6 is 110.
  • Invert 0’s and 1’s. So, the new binary representation would be 001.
  • Now, add 1 to it.
  • After adding 1, we will get 010.

So, the 2’s complement of -6 would be 010. This is when we have only considered 3 bits. But if we have considered 32 bits, then 2’s complement of -6 would be 11111111111111111111111111111010.

Let’s first calculate the bit count for positive numbers

Let’s take an integer value = 6. The binary representation of 6 is 110, so the count of 1 bit should be 2.

public class Codekru {

	public static void main(String[] args) {

		int i = 6;

		System.out.println("Binary representation of i: " + Integer.toBinaryString(i));

		System.out.println("Number of 1 bits: " + Integer.bitCount(i));
	}
}

Output –

Binary representation of i: 110
Number of 1 bits: 2
Now, let’s calculate the bit count for the negative numbers

Let’s take the negative integer number = -6. The 2’s complement binary representation of -6 is 11111111111111111111111111111010. Thus the bit count for -6 would be 30.

public class Codekru {

	public static void main(String[] args) {

		int i = -6;

		System.out.println("Binary representation of i: " + Integer.toBinaryString(i));

		System.out.println("Number of 1 bit: " + Integer.bitCount(i));
	}
}

Output –

Binary representation of i: 11111111111111111111111111111010
Number of 1 bit: 30
Time Complexity of the bitCount() method

The time complexity of the Integer bitCount() method is O(1) as it performs only constant operations on the input. Below is the internal implementation of the bitCount() method –

public static int bitCount(int i) {
        // HD, Figure 5-2
        i = i - ((i >>> 1) & 0x55555555);
        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
        i = i + (i >>> 8);
        i = i + (i >>> 16);
        return i & 0x3f;
    }
Q- What if we passed a null Integer value to bitCount() argument?

It will throw a NullPointerException as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) {

		Integer i = null;

		System.out.println("Number of 1 bit: " + Integer.bitCount(i));
	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "i" is null

Please visit this link to learn more about the Integer wrapper class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *