Guest User

Bits Sifting

a guest
Apr 11th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. /* Problem 21.  ** – Bit Sifting
  2. This problem is from Variant 3 of C# Basics exam from 11-04-2014 Morning.  You can test your solution here .
  3. In this problem we'll be sifting bits through sieves (sift = пресявам, sieve = сито).
  4. You will be given an integer, representing the bits to sieve, and several more numbers, representing the sieves the bits will fall through.
  5.  * Your task is to follow the bits as they fall down, and determine what comes out of the other end.
  6. Example
  7. For this example, imagine we are working with 8-bit integers (the actual problem uses 64-bit ones).
  8.  * Let the initial bits be given as 165 (10100101 in binary), and the sieves be 138 (10001010), 84 (01010100) and 154 (10011010).
  9.  * The 1 bits from the initial number fall through the 0 bits of the sieves and stop if they reach a 1 bit; if they make it to the end,
  10.  * they become a part of the final number.
  11. In this case, the final number is 33 (00100001), which has two 1 bits in its binary form – the answer is 2.  
  12. 10100101
  13. ↓ ↓  ↓ ↓
  14. 10001010
  15.   ↓  ↓ ↓
  16. 01010100
  17.   ↓    ↓
  18. 10011010
  19.   ↓    ↓
  20. 00100001
  21. Input
  22. The input data should be read from the console.
  23. • On the first line of input, you will read an integer representing the bits to sieve.
  24. • On the second line of input, you will read an integer N representing the number of sieves.
  25. • On the next N lines of input, you will read N integers representing the sieves.
  26. The input data will always be valid and in the format described. There is no need to check it.
  27. Output
  28. The output must be printed on the console.
  29. On the single line of the output you must print the count of "1" bits in the final result.
  30.  */
  31.  
  32. using System;
  33. using System.Linq;
  34. using System.Text.RegularExpressions;
  35. using System.Numerics;
  36.  
  37. class BitsSifting
  38. {
  39.     static void Main()
  40.     {
  41.         // input;
  42.         ulong number = ulong.Parse(Console.ReadLine());
  43.         int n = int.Parse(Console.ReadLine());
  44.  
  45.         ulong sieve;
  46.         ulong mask;
  47.         for (int i = 0; i < n; i++)
  48.         {
  49.             sieve = ulong.Parse(Console.ReadLine());
  50.             mask = number & sieve;
  51.             number ^= mask;
  52.         }
  53.  
  54.         int counter = 0;
  55.  
  56.     // first "1"-bits counting algorithm
  57.         for (int i = 0; i < 64; i++)
  58.         {
  59.             ulong result = number & 1;
  60.             if (result == 1)
  61.             {
  62.                 counter++;
  63.             }
  64.             number = number >> 1;
  65.         }
  66.  
  67.     // second "1"-bits counting algorithm
  68.         //- comment the upper one and uncomment this one if you want to try it
  69.         //for (int i = 0; i < 64; i++)
  70.         //{
  71.         //    mask = (ulong)1 << i;
  72.         //    ulong result = number & mask;
  73.         //    if (result == mask)
  74.         //    {
  75.         //        counter++;
  76.         //    }
  77.         //}
  78.  
  79.     // all "1"-bits counting algorithms are string-based
  80.     // for them to work we need a binary string representation of our number
  81.         //ulong remainder;
  82.         //string binary = string.Empty;
  83.         //while (number > 0)
  84.         //{
  85.         //    remainder = number % 2;
  86.         //    number /= 2;
  87.         //    binary = remainder.ToString() + binary;
  88.         //}
  89.  
  90.     // third "1"-bits counting algorithm
  91.         //- comment the upper one and uncomment this one if you want to try it
  92.         //for (int i = 0; i < binary.Length; i++)
  93.         //{
  94.         //    if (binary[i] == '1')
  95.         //    {
  96.         //        counter++;
  97.         //    }
  98.         //}
  99.  
  100.     // forth "1"-bits counting algorithm
  101.         //- comment the upper one and uncomment this one if you want to try it
  102.         //counter = binary.Count(x => x == '1');
  103.  
  104.     // fifth "1"-bits counting algorithm
  105.         //- comment the upper one and uncomment this one if you want to try it
  106.         //counter = binary.Where(x => x == '1').Count();
  107.  
  108.     // sixth "1"-bits counting algorithm
  109.         //- comment the upper one and uncomment this one if you want to try it
  110.         //counter = binary.Split('1').Length - 1;
  111.  
  112.     // seventh "1"-bits counting algorithm
  113.         //- comment the upper one and uncomment this one if you want to try it
  114.         //counter = binary.Length - (binary.Replace("1", "").Length);
  115.  
  116.     // eighth "1"-bits counting algorithm
  117.         //- comment the upper one and uncomment this one if you want to try it
  118.         //string pattern = "1";
  119.         //counter = Regex.Matches(binary, pattern).Count;
  120.  
  121.         Console.WriteLine(counter);
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment