Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class BinaryDigitsCountProgram
- {
- static void Main()
- {
- Console.WriteLine("Enter byte 0 or 1 , B = ");
- int b = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter count of number , N = ");
- int n = int.Parse(Console.ReadLine());
- uint bit = 0;
- uint mask = 0;
- if (b == 1 || b == 0)
- {
- for (int j = 0; j < n; j++)
- {
- Console.WriteLine("Enter number P = ");
- 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 0
- {
- count0 = count0 + 1;
- }
- }
- if (b == 1)
- {
- Console.WriteLine("The number {0} have {1} bytes \"1\"", p, count1);
- }
- else
- {
- Console.WriteLine("The number {0} have {1} bytes \"0\"", p, count0);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement