Java Program to Count set bits in an integer Last Updated : 14 May, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Write an efficient program to count number of 1s in binary representation of an integer. Examples :Input : n = 6Output : 2Binary representation of 6 is 110 and has 2 set bitsInput : n = 13Output : 3Binary representation of 11 is 1101 and has 3 set bits1. Simple Method Loop through all bits in an integer, check if a bit is set and if it is then increment the set bit count. See below program. Java // Java program to Count set // bits in an integer import java.io.*; class countSetBits { /* Function to get no of set bits in binary representation of positive integer n */ static int countSetBits(int n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } // driver program public static void main(String args[]) { int i = 9; System.out.println(countSetBits(i)); } } // This code is contributed by Anshika Goyal. Output2 Recursive Approach : Java // Java implementation of recursive // approach to find the number // of set bits in binary representation // of positive integer n import java.io.*; class GFG { // recursive function to count set bits public static int countSetBits(int n) { // base case if (n == 0) return 0; else // if last bit set add 1 else add 0 return (n & 1) + countSetBits(n >> 1); } // Driver code public static void main(String[] args) { // get value from user int n = 9; // function calling System.out.println(countSetBits(n)); } } // This code is contributes by sunnysingh Output2 Please refer complete article on Count set bits in an integer for more details! Comment More infoAdvertise with us Next Article Java Integer bitCount() Method K kartik Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Java Integer bitCount() Method The bitCount() method of Integer class of java.lang package returns the count of set bits in a positive number. For negative numbers, it returns the count of set bits in its 2s complement form. This function is sometimes referred to as the population count. Syntax:public static int bitCount(int n)Pa 2 min read BigInteger bitCount() Method in Java Prerequisite: BigInteger Basics The java.math.BigInteger.bitCount() method returns number of bits in the two's complement representation of this BigInteger that differ from its sign bit. This method is useful when implementing bit-vector style sets atop BigIntegers.Syntax: public int bitCount() Para 1 min read BigInteger setBit() Method in Java The java.math.BigInteger.setbit(index) method returns a Big-integer whose value is equivalent to this Big-integer with the designated bit set. The method computes (this | (1<<n)). The bit at index n of binary representation of Big-integer will be set means converted to 1. Syntax: public BigInt 2 min read BitSet nextSetBit() method in Java BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. Prerequisite : Java BitSet | Set 1 nextSetBit() method : This method in BitSet Class is used to return the index of the first bit that is set to true, that occurs on or after the specified 3 min read BigInteger testBit() Method in Java prerequisite : BigInteger Basics The java.math.BigInteger.testBit(index) method returns true if and only if the designated bit is set. This method Computes (this & (1<<n)) != 0). Syntax: public boolean testBit(int n) Parameter: The method takes one parameter n of integer type which refers 2 min read BitSet previousSetBit() method in Java BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. Prerequisite : Java BitSet | Set 1 Bitset.previousSetBit() This method is used to find whether there are any true bits that occur on or before the specified starting index. This function re 3 min read Like