SHARE
TWEET

BinaryDigitsCount

a guest Nov 4th, 2012 140 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top