Count Set Bits in an Integer in Java



In this article, we are given an integer value and the task is to count the total number of set bits of the given integer. For this task, we need to convert the given value into its corresponding binary representation.

The binary number of an integer value is represented as the combination of 0's and 1's. Here, the digit 1 is known as the Set bit in the terms of the computer.

Problem Statement

Write a program in Java to count set bits in an integer ?

Input 

int num = 10

Output

binary representation = 1010
set bit count = 2

As the number of 1's in the binary equivalent of the given integer value is 2, the total number of set bits is 2.

Different approaches

Following are the different approaches to counting set bits in an integer ?

Count Set Bits using Brian Kernighan's Algorithm

In Brian Kernighan's algorithm, we use a while loop which iterates through each bit of binary number and unsets the rightmost set bit of the number. On counting how many times this operation is performed until the number becomes zero will give the total number of set bits in an integer.

  • Define a method set_bits_count(int num) that takes an integer as input.
  • Initialize a variable count to 0 and through this we can keep the track of the number of set bits.
  • Use a while loop that runs as long as num is greater than 0.
  • Inside the loop, perform the operation num &= (num - 1); to turn off the rightmost set bit.
  • Increment the count variable each time the operation is performed and continue the loop until num becomes 0.
  • Return the value of count which represents the total number of set bits.
  • In the main method, we will initialize an integer variable num with a value.
  • Call the set_bits_count(num) method and print the result.

Example

In this example, we are using the Brian Kernighan algorithm to count set bits ?

import java.io.*;
public class Demo{
   static int set_bits_count(int num){
      int count = 0;
      while (num > 0){
         num &= (num - 1);
         count++;
      }
      return count;
   }
   public static void main(String args[]){
      int num =11;
      System.out.println("The number of set bits in 11 is ");
      System.out.println(set_bits_count(num));
   }
}

Output 

The number of set bits in 11 is
3

Count Set Bits using Bitwise Operation

In this approach, use bitwise AND (&) with 1 to check if the least significant bit of the given integer value is set or not. If it is set, increment the count of the set bit and then right-shift the given integer value by one position.

  • In the main method, initialize an integer variable intVal with a given integer value.
  • Initialize a variable totCount to 0. This will keep track of the number of set bits.
  • Use a while loop that runs as long as intVal is greater than 0.
  • Inside the loop, use the bitwise AND operator (&) with 1 to check if the least significant bit is set. Add the result to totCount.
  • Right-shift the intVal by one position using the >>= operator to move to the next bit.
  • Continue the loop until intVal becomes 0.
  • Print the value of totCount, which represents the total number of set bits.

Example

In the following example, we are counting the total number of set bits using the bitwise operation ?

public class Main {
   public static void main(String[] args) {
      // given integer value
      int intVal = 15;  
      int totCount = 0;
	  // counting set bits
      while (intVal > 0) {
         totCount += intVal & 1;
         intVal >>= 1;
      }
      System.out.println("Total count of set bits: " + totCount);
   }
}

Output

Total count of set bits: 4

Count Set Bits using Integer.bitCount()

The Integer.bitCount() method takes an integer value as a parameter and returns the count of 1's bit in the binary representation of the specified integer value.

  • In the main method, initialize an integer variable intVal with a given integer value.
  • Use the Integer.bitCount(intVal) method to count the number of set bits in the binary representation of intVal.
  • Store the result in a variable totCount.
  • Print the value of totCount, which represents the total number of set bits.

Example

The following example shows how to use Integer.bitCount() method to count the total number of bits in an integer ?

public class Main {
   public static void main(String[] args) {
      // given integer value
      int intVal = 14; 
      // using the bitcount() method
      int totCount = Integer.bitCount(intVal);
      // printing the result
      System.out.println("Total count of set bits = " + totCount);
   }
}

Output

Total count of set bits = 3
Updated on: 2024-09-11T11:15:02+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements