Java Program to Count set bits in an integer Last Updated : 14 May, 2025 Comments Improve Suggest changes 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 Program to Count set bits in an integer 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 How to Compute n % 2^k in Java Using Binary and Bitwise Methods? In Java, sometimes we need to divide one number by another number to find out the remainder. But the most interesting case is when we need to divide by power of 2. The most standard approach to do this using the modulus operator (%) and the fastest approach is to do this by bitwise operations.What i 4 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 Java lang.Long.builtcount() method in Java with Examples java.lang.Long.bitCount() is a built in function in Java that returns the number of set bits in a binary representation of a number. It accepts a single mandatory parameter number whose number of set bits is returned. Syntax: public static long bitCount(long num) Parameters: num - the number passed 3 min read BigInteger bitLength() Method in Java The java.math.BigInteger.bitLength() method returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit. For positive BigIntegers, this is equivalent to the number of bits in the ordinary binary representation. The bitLength method Computes (cei 1 min read Java.util.BitSet class in Java with Examples | Set 1 BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. Constructors: BitSet class Constructors / \ BitSet() BitSet(int no_Of_Bits)BitSet() : A no-argument constructor to create an empty BitSet object. BitSet(int no_Of_Bits): A one-constructor w 2 min read Like