Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class BinaryDigitsCount
- {
- static void Main(string[] args)
- {
- byte bitGivenValue = byte.Parse(Console.ReadLine());
- ushort numN = ushort.Parse(Console.ReadLine());
- uint [] integers = new uint [numN];
- for (int i = 0; i < integers.Length; i++)
- {
- integers[i] = uint.Parse(Console.ReadLine());
- }
- for (int i = 0; i < integers.Length; i++)
- {
- uint number = integers[i];
- uint countBitsCoin = CountValueOfBitsInPositiveInteger(number, bitGivenValue);
- integers[i] = countBitsCoin;
- }
- foreach (var item in integers)
- {
- Console.WriteLine(item);
- }
- }
- static uint CountValueOfBitsInPositiveInteger (uint number, byte bitsGivenValue)
- {
- uint countBits = 0;
- uint quotient = 0;
- byte remainder = 0;
- checked
- {
- do
- {
- quotient = number / 2;
- remainder = (byte)(number % 2);
- if (remainder == bitsGivenValue)
- {
- countBits++;
- }
- number = quotient;
- } while (number > 0);
- }
- return countBits;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement