Javaexercise.com

Java Integer bitCount() Method

Java bitCount() method is used to get the frequency of one-bit number in the binary representation of the int value. It means this method returns a count of one-bits present in a binary. Since this method takes an integer value as an argument then compiler first converts that value into a binary number and then count number of one-bits present in this binary.

Internally it converts the specified int value into the two's complement binary.

This method takes an integer argument and returns an integer too. This function is sometimes referred to as the population count of one-bits in a binary number.

Java bitCount() method belongs to the Integer class which is a wrapper class.

This method is useful in counting the number of 1's in the given bit sequence. For example, if given input is 5 then this method will return 2 as there are 2 one-bits in the binary representation (101) of this input.

 

Java bitCount() Method Syntax:

public static int bitCount(int i)

 

Parameters:

It takes a single argument of int type.

Return:

This method returns a primitive integer value that represents the one-bit number present in two's complement binary form of the specified integer number.

Exception:

It does not throw any exception.

Version:

Since 1.5

 

Example: Count frequency one-bit in a binary

To count one-bit frequency in a binary number, Java provides a bitCount() method. In this example, we have an integer value as 10 and getting a count of one-bit in its binary.

import java.lang.Integer;

/* 
 * Example to count one-bits in a binary number
 */
public class JExercise {
	public static void main(String[] args) {

		int a = 10;
		System.out.println("Input value is: "+a);
		// count the one-bits
		int result = Integer.bitCount(a);
		// Display result
		System.out.println("one-bits frequency in its binary: "+result);
		
		
	}
}


Output:

Input value is: 10
one-bits frequency in its binary: 2

 

You may be surprised how does it find and count one-bits in the int number. You can refer toBinaryString() method of Integer class to get binary of any integer value. See, we did that in the below example.

Example: Binary Equevelent of an integer value

import java.lang.Integer;

/* 
 * Example to count one-bits in a binary number
 */
public class JExercise {
	public static void main(String[] args) {

		int a = 10;
		System.out.println("Input value is: "+a);
		// Getting binary of an int value
		System.out.println("Binary of "+a+": "+Integer.toBinaryString(a));
		// count the one-bits
		int result = Integer.bitCount(a);
		// Display result
		System.out.println("one-bits frequency in its binary: "+result);
		
		
	}
}

Output:

Input value is: 10
Binary of 10: 1010
one-bits frequency in its binary: 2
 

 


Conclusion

Well, in this topic, we have learned to count one-bits present in a binary of an integer value. We used bitCount() method of Integer class in our examples to get frequency of one-bits in a binary.

If we missed something, you can suggest us at - info.javaexercise@gmail.com