Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

Java Integer bitCount() Method

Java bitCount() method belongs to the Integer class. This method is used to return a one-bit number into two's complement binary form of the specified integer value and counts a number of set bits in a binary sequence.

This method takes an integer as a parameter and returns an integer too. For example, if the given input is 1000111110 than this method should return 6, as there are 6 set bits(1) in this input.

Syntax:

public static int bitCount(int i)

Parameters:

The only parameter that is passed is the integer, of which two's complement binary form will be returned.

Returns:

This method returns the one-bit number into two's complement binary form of the integer number, hence its return type is also int.

Example 1:

Here, we are entering an integer number 365. The Integer.toBinaryString() method will convert the number into its equivalent binary string (101101101 for the integer 365). Then the converted binary number is passed through Integer.countBit() to count the number of bits.

import java.lang.Integer;

public class StudyTonight {
  public static void main(String[] args) {
    int i = 365;
    int j = -365;
    System.out.println("Actual Number is " + i);

    // converting the number to its equivalent binary form with base 2 
    System.out.println("Binary conversion of" + i + " = " + Integer.toBinaryString(i));

    //returning the number of one-bits 
    System.out.println("Number of one bits = " + Integer.bitCount(i));
    System.out.println("*******************");
    System.out.println("Actual Number is " + j);
    System.out.println("Binary conversion of" + j + " = " + Integer.toBinaryString(j));
    System.out.println("Number of one bits = " + Integer.bitCount(j));
  }
}


Actual Number is 365
Binary conversion of365 = 101101101
Number of one bits = 6
*******************
Actual Number is -365
Binary conversion of-365 = 11111111111111111111111010010011
Number of one bits = 27

Example 2:

Here, we are taking an array of integers. The Integer.toBinaryString() method will convert the numbers in the array into its equivalent binary String. The binary string needs to be converted into Integer before passing it through the countBit() method, This is done through Integer.parseInt() method Then the converted binary number is passed through Integer.countBit() to count the number of bits.

import java.lang.Integer;

public class StudyTonight {
  public static void main(String args[]) {
    int[] set = {
      72,
      78,
      56,
      89,
      80,
      66
    };
    try {
      for (int i = 0; i < 6; i++) {
        String b = Integer.toBinaryString(set[i]); //Converting the actual number to binary String
        int c = Integer.parseInt(b, 2); //Converting the binary String to binary integer of base 2
        int d = Integer.bitCount(c); //Counting the set bits
        System.out.println("Actual number is " + set[i] + " binary sequence : " + b + ",number of set bits are : " + d);
      }
    }
    catch(Exception e) {
      System.out.println("Exception occurred..");
    }
  }
}


Actual number is 72 binary sequence : 1001000,number of set bits are : 2
Actual number is 78 binary sequence : 1001110,number of set bits are : 4
Actual number is 56 binary sequence : 111000,number of set bits are : 3
Actual number is 89 binary sequence : 1011001,number of set bits are : 4
Actual number is 80 binary sequence : 1010000,number of set bits are : 2
Actual number is 66 binary sequence : 1000010,number of set bits are : 2

Example 3:

Here is a general example where the user can enter the number of his choice and get the desired output. The try and catch block is used for handling any exception taking place during the execution of the program. (For example, the user enters a String, etc. in place of the integer).

import java.lang.Integer;
import java.util. * ;

public class StudyTonight {

  public static void main(String[] args) {

    System.out.println("Enter number");
    Scanner sc = new Scanner(System. in );
    try {
      int i = sc.nextInt();
      System.out.println("Actual Number is " + i);

      // converting the number to its equivalent binary form with base 2 
      System.out.println("Binary conversion of" + i + " = " + Integer.toBinaryString(i));

      //returning the number of one-bits 
      System.out.println("Number of one bits = " + Integer.bitCount(i));
    }
    catch(Exception e) {
      System.out.println("Error!!");
    }
  }
}


Enter number
67
Actual Number is 67
Binary conversion of 67 = 1000011
Number of one bits = 3
**************************************
Enter number
0x896
Error!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.