In this Java tutorial, you will learn about Integer.bitCount() method, and how to use this method to find the number of one-bits in specified integer, with the help of examples.

## Integer.bitCount()

Integer.bitCount() returns the number of one-bits in the two’s complement binary representation of the specified int value.

## Syntax

The syntax of bitCount() method is

`Integer.bitCount(int i)`

where

Returns

The method returns value of type int.

## Examples

### 1. Bit count of an integer

In this example, we will take an integer `a` and find the number of one-bits in the integer value using `Integer.bitCount()` method.

Java Program

```public class Example{
public static void main(String[] args){
int a = 7;
int count = Integer.bitCount(a);
System.out.println("Bit count of " + a + " is: " + count);
}
}```

Output

`Bit count of 7 is: 3`

Explanation

```7 = 00000111
Numbers of bits with value 1 is 3.```

### 2. Bit count of multiple integers

In this example, we will find the one-bit count for multiple integers.

Java Program

```public class Example{
public static void main(String[] args){
System.out.println("Bit count of 1 is: " + Integer.bitCount(1)); // 00000001
System.out.println("Bit count of 2 is: " + Integer.bitCount(2)); // 00000010
System.out.println("Bit count of 3 is: " + Integer.bitCount(3)); // 00000011
System.out.println("Bit count of 4 is: " + Integer.bitCount(4)); // 00000100
System.out.println("Bit count of 5 is: " + Integer.bitCount(5)); // 00000101
System.out.println("Bit count of 6 is: " + Integer.bitCount(6)); // 00000110
System.out.println("Bit count of 7 is: " + Integer.bitCount(7)); // 00000111
System.out.println("Bit count of 8 is: " + Integer.bitCount(8)); // 00001000
System.out.println("Bit count of 9 is: " + Integer.bitCount(9)); // 00001001
System.out.println("Bit count of 10 is: " + Integer.bitCount(10)); // 00001010
}
}```

Output

```Bit count of 1 is: 1
Bit count of 2 is: 1
Bit count of 3 is: 2
Bit count of 4 is: 1
Bit count of 5 is: 2
Bit count of 6 is: 2
Bit count of 7 is: 3
Bit count of 8 is: 1
Bit count of 9 is: 2
Bit count of 10 is: 2```

Explanation

```7 = 00000111
Numbers of bits with value 1 is 3.```

## Conclusion

In this Java Tutorial, we have learnt the syntax of Java Integer.bitCount() method, and also how to use this method with the help of Java example programs.