C# Program for 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. C# // C# program to Count set // bits in an integer using System; class GFG { // 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 Code public static void Main() { int i = 9; Console.Write(countSetBits(i)); } } // This code is contributed by Sam007 Output2 Recursive Approach : C# // C# implementation of recursive // approach to find the number of // set bits in binary representation // of positive integer n using System; 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 static public void Main() { // get value // from user int n = 9; // function calling Console.WriteLine(countSetBits(n)); } } // This code is contributed by aj_36 Output2 Please refer complete article on Count set bits in an integer for more details! Comment More infoAdvertise with us Next Article C# Program for Count set bits in an integer kartik Follow Improve Article Tags : C# Similar Reads Counting Set bit in C In C programming, counting set bits is the process of determining the number of bits that are set to 1 in a binary number. This operation is useful in various applications including network programming, error detection, and cryptography.In this article, we will learn how to count the number of set b 4 min read Count set bits in an integer Write an efficient program to count the number of 1s in the 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 13 is 1101 and has 3 set bits[Naive Approach] - One by One CountingTh 15+ min read How to Count Set Bits in an Integer in C++? In binary representation of a number, a set bit is defined as the binary digit (bit) that is set to 1. In this article, we will learn how to count the set bits in a given integer in C++.ExampleInput: 13Output:The number of set bits in 13 (1101) is: 3Counting Set Bits in an IntegerTo count the set bi 2 min read C/C++ Program to Count set bits in an integer 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 int 2 min read How to count set bits in a floating point number in C? Given a floating point number, write a function to count set bits in its binary representation. For example, floating point representation of 0.15625 has 6 set bits (See this). A typical C compiler uses single precision floating point format. We can use the idea discussed here. The idea is to take a 2 min read Number of integers with odd number of set bits Given a number n, count number of integers smaller than or equal to n that have odd number of set bits.Examples: Input : 5 Output : 3 Explanation : Integers with odd number of set bits in range 1 to 5 : 0 contains 0 set bits 1 contains 1 set bits 2 contains 1 set bits 3 contains 2 set bits 4 contain 6 min read Java Program to Count set bits in an integer 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 int 2 min read Program to count number of set bits in an (big) array Given an integer array of length N (an arbitrarily large number). How to count number of set bits in the array?The simple approach would be, create an efficient method to count set bits in a word (most prominent size, usually equal to bit length of processor), and add bits from individual elements o 12 min read Count unset bits of a number Given a number n, count unset bits after MSB (Most Significant Bit).Examples : Input : 17 Output : 3 Binary of 17 is 10001 so unset bit is 3 Input : 7 Output : 0 A Simple Solution is to traverse through all bits and count unset bits. C++ // C++ program to count unset bits in an integer #include < 7 min read Count total set bits in all numbers from 1 to N | Set 3 Given a positive integer N, the task is to count the total number of set bits in binary representation of all the numbers from 1 to N. Examples: Input: N = 3 Output: 4 setBits(1) + setBits(2) + setBits(3) = 1 + 1 + 2 = 4 Input: N = 6 Output: 9 Approach: Solution to this problem has been published in 12 min read Like