Advertisement
G_Burlakova

BinaryDigitsCount

Mar 31st, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 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.  
  8. class BinaryDigitsCount
  9. {
  10.     static void Main(string[] args)
  11.     {
  12.         uint BInput = uint.Parse(Console.ReadLine());
  13.         uint B = 0;
  14.         uint N = uint.Parse(Console.ReadLine());
  15.         uint currentNum;
  16.         int counter;
  17.         uint[] numbers = new uint[N];
  18.         if (BInput == 0)
  19.         {
  20.             B = BInput + 1;
  21.         }
  22.         else
  23.         {
  24.             B = 1;
  25.         }
  26.         for (int i = 0; i < N; i++)
  27.         {
  28.             numbers[i] = uint.Parse(Console.ReadLine());
  29.         }
  30.         for (int i = 0; i < N; i++)
  31.         {
  32.             counter = 0;
  33.             currentNum = numbers[i];
  34.             if (BInput == 1)
  35.             {
  36.                 while (currentNum != 0)
  37.                 {
  38.                     if ((currentNum & B) == 1)
  39.                     {
  40.                         counter++;
  41.                     }
  42.                     currentNum = currentNum >> 1;
  43.                 }
  44.             }
  45.             else
  46.             {
  47.                 while (currentNum != 0)
  48.                 {
  49.                     if ((currentNum & B) != 1)
  50.                     {
  51.                         counter++;
  52.                     }
  53.                     currentNum = currentNum >> 1;
  54.                 }
  55.             }
  56.             Console.WriteLine(counter);
  57.         }
  58.        
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement