lmarkov

Binary Digits Count

Dec 14th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace BinaryDigitsCount
  8. {
  9.     class BinaryDigitsCount
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             byte numberB = byte.Parse(Console.ReadLine());
  14.             short numberN = short.Parse(Console.ReadLine());
  15.  
  16.             for (int i = 0; i < numberN; i++)
  17.             {
  18.                 int counter = 0;
  19.                 uint currentNumber = uint.Parse(Console.ReadLine());
  20.  
  21.                 while (currentNumber != 0)
  22.                 {
  23.                     if((currentNumber & 1) == numberB)
  24.                     {
  25.                         counter++;
  26.                     }
  27.                     currentNumber >>= 1;
  28.                 }
  29.                 Console.WriteLine(counter);
  30.             }
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment