Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class BinaryDigitsCount
- {
- static void Main()
- {
- int b = int.Parse(Console.ReadLine());
- int n = int.Parse(Console.ReadLine());
- uint bit = 0;
- uint mask = 0;
- if (b == 1 || b == 0)
- {
- for (int j = 0; j < n; j++)
- {
- uint p = uint.Parse(Console.ReadLine());
- uint count1 = 0;
- uint count0 = 0;
- for (int i = 0; i < 32; i++)
- {
- //chacking which bits stay of positions p - 0 or 1
- mask = (uint)(1 << i);
- bit = (uint)((p & mask) >> i);
- if (bit == 1) //counting how meny bits are 1
- {
- count1 = count1 + 1;
- }
- else if (bit == 0 && mask < p) //counting how meny bits are 1
- {
- count0 = count0 + 1;
- }
- }
- if (b == 1)
- {
- Console.WriteLine("{0}", count1);
- }
- else
- {
- Console.WriteLine("{0}", count0);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement