Advertisement
dimipan80

Exam 3. Bit Sifting

Jun 7th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. namespace _5.BitSifting_Bitwise
  2. {
  3.     using System;
  4.    
  5.     public class BitSifting
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             checked
  10.             {
  11.                 ulong initialBits = ulong.Parse(Console.ReadLine());
  12.                 int countSieves = int.Parse(Console.ReadLine());
  13.                
  14.                 for (int i = 0; i < countSieves; i++)
  15.                 {
  16.                     ulong sieve = ulong.Parse(Console.ReadLine());
  17.                     initialBits = (initialBits ^ sieve) & initialBits;
  18.                 }
  19.  
  20.                 // Console.WriteLine("Result num: " + initialBits);
  21.                 // Console.WriteLine("In Binary string: " + Convert.ToString((long)initialBits, 2));
  22.                 int bitCounter = 0;
  23.                 for (int bit = 0; bit < 64; bit++)
  24.                 {                    
  25.                     if ((initialBits & 1) > 0)
  26.                     {
  27.                         bitCounter++;
  28.                     }
  29.  
  30.                     initialBits >>= 1;
  31.                 }
  32.  
  33.                 Console.WriteLine(bitCounter);
  34.             }
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement