iliya87

05.BitSifting

Mar 23rd, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.63 KB | None | 0 0
  1. using System;
  2.  
  3. class BitSifting
  4. {
  5.     static void Main()
  6.     {
  7.         ulong bits = ulong.Parse(Console.ReadLine());
  8.         int sieves = int.Parse(Console.ReadLine());
  9.  
  10.         for (int i = 0; i < sieves; ++i)
  11.         {
  12.             ulong sieve = ulong.Parse(Console.ReadLine());
  13.             bits = (bits ^ sieve) & bits;
  14.         }
  15.  
  16.         // Now count the bits. There are many ways to do this...
  17.         // Here we're using Brian Kernighan's hack
  18.         int bitCount = 0;
  19.         while (bits != 0)
  20.         {
  21.             bits &= (bits - 1);
  22.             bitCount += 1;
  23.         }
  24.         Console.WriteLine(bitCount);
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment