Advertisement
Guest User

BinaryDigitsCount

a guest
Nov 4th, 2012
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  1. using System;
  2.  
  3. class BinaryDigitsCount
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         int goalBit = int.Parse(Console.ReadLine());
  8.         int n = int.Parse(Console.ReadLine());
  9.  
  10.         for (int i = 0; i < n; i++)
  11.         {
  12.             int numOfGoalBits = 0;
  13.             uint num = uint.Parse(Console.ReadLine());
  14.            
  15.             int firstNonZeroBit = 31;
  16.             while ((num & (1 << firstNonZeroBit)) == 0)
  17.             {
  18.                 firstNonZeroBit--;
  19.             }
  20.             while (firstNonZeroBit >= 0)
  21.             {
  22.                 if ((num & 1) == goalBit)
  23.                 {
  24.                     numOfGoalBits++;
  25.                 }
  26.                 num >>= 1;
  27.                 firstNonZeroBit--;
  28.             }
  29.             Console.WriteLine(numOfGoalBits);
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement