C# Program for Count set bits in an integer (original) (raw)
Last Updated : 14 May, 2025
Write an efficient program to count number of 1s in binary representation of an integer.
**Examples :
Input : n = 6
Output : 2
Binary representation of 6 is 110 and has 2 set bits
Input : n = 13
Output : 3
Binary representation of 11 is 1101 and has 3 set bits
**1. 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
`
**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
`
Please refer complete article on
for more details!
Similar Reads
- C# | Number of elements contained in the BitArray The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.Count property is used to get the number of element 2 min read
- C# | Set all bits in the BitArray to the specified value The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.SetAll(Boolean) method is used to set all bits in t 3 min read
- C# | Set the bit at a specific position in the BitArray to the specified value The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.Set(Int32, Boolean) method is used to set the bit a 3 min read
- C# BitArray Class BitArray class in C# is part of the System.Collections namespace. It manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e. 1, and false indicates the bit is off i.e. 0. The BitArray class is a collection class in which the capacity is 6 min read
- 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