Do you want to count the number of 1’s in a number in C# ? . Below is a sample source code that demonstrates how to count the number of 1 bits.
How to Count the Number of 1 Bits in C# ?
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace AbundantcodeConsoleApp { internal class Program { private static void Main(string[] args) { // How to Count the Number of 1 Bits in C# ? int Input = 5; int NoOfBits = 0; while (Input > 0) { Input &= (Input - 1); NoOfBits++; } Console.WriteLine(NoOfBits); Console.ReadLine(); } } }
Doesn’t always work.
Try 10 as input for instance