sashomaga

Untitled

Dec 22nd, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. //One of the things the programmers love the most is bitwise operations. The "bitwise guy" is a synonym for a programmer that loves bits more than everything
  3. //else in programming. Mitko is a "bitwise guy". He invented a new bitwise algorithm. The algorithm takes one positive integer number P, makes magic with it
  4. //and returns a new positive integer number. He also defined a new number P̃ which represents the number P in binary numeral system with inverted bits. All zeros
  5. //in P are ones in P̃ and all ones in P are zeros in P̃. For example if we have P = 9 (which is 1001 in binary numeral system) its inverted number P̃ will be equal
  6. //to 6 (which is 110 in binary numeral system). But that’s not all! He invented another number P̈, which represents reversed number P in binary numeral system.
  7. //For example if we have P = 11 (which is 1011 in binary numeral system) its reversed number P̈ is equal to 13 (which is 1101 in binary numeral system).
  8. //The Mitko's magical algorithm takes a number P and transforms it to a new number Pnew using the following bitwise transformation: Pnew = (P ^ P̃) & P̈.
  9. class Program
  10. {
  11.     static void Main()
  12.     {
  13.         int n = int.Parse(Console.ReadLine());
  14.         int[] p = new int[n];
  15.         int[] p1 = new int[n];
  16.         int[] p2 = new int[n];
  17.         int[] res = new int[n];
  18.         for (int i = 0; i < p.Length; i++)
  19.         {
  20.             p[i] = int.Parse(Console.ReadLine());
  21.             p1[i] = GetP1(p[i]);
  22.             p2[i] = GetP2(p[i]);
  23.             res[i] = (p[i] ^ p1[i]) & p2[i];
  24.         }
  25.         foreach (int answer in res)
  26.         {
  27.             Console.WriteLine(answer);
  28.         }
  29.        
  30.     }
  31.  
  32.     private static int GetP2(int p)
  33.     {
  34.         int res = 0;
  35.         string length = Convert.ToString(p, 2);
  36.         for (int i = length.Length-1, j = 0; i >= 0; i--, j++)
  37.         {
  38.             if (((p >> i) & 1) == 1)
  39.             {
  40.                 res = res | (1 << j);
  41.             }
  42.         }
  43.         return res;
  44.     }
  45.  
  46.     private static int GetP1(int p)
  47.     {    
  48.         int res = 0;
  49.         string length = Convert.ToString(p, 2);
  50.         for (int i = 0; i < length.Length; i++)
  51.         {
  52.            
  53.             if (((p >> i)&1) == 0)
  54.             {
  55.                 res = res | (1 << i);
  56.             }
  57.         }
  58.             return res;
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment