TheBulgarianWolf

Binary Digits Count

Dec 30th, 2020
1,336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. using System;
  2.  
  3. namespace BitwiseOperations
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.WriteLine("This program will calculate the number of the given binary digit in your number");
  10.             Console.WriteLine("Enter your number: ");
  11.             int number = int.Parse(Console.ReadLine());
  12.             Console.WriteLine("Enter '0' or '1' : ");
  13.             int binaryNumber = int.Parse(Console.ReadLine());
  14.             int count = 0;
  15.             while(number> 0)
  16.             {
  17.                 int binaryDigit = number % 2;
  18.                 if(binaryDigit == binaryNumber)
  19.                 {
  20.                     count++;
  21.                 }
  22.                 number /= 2;
  23.             }
  24.             Console.WriteLine(count);
  25.         }
  26.     }
  27. }
  28.  
Add Comment
Please, Sign In to add comment