Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //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
- //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
- //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
- //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
- //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.
- //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).
- //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̈.
- class Program
- {
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- int[] p = new int[n];
- int[] p1 = new int[n];
- int[] p2 = new int[n];
- int[] res = new int[n];
- for (int i = 0; i < p.Length; i++)
- {
- p[i] = int.Parse(Console.ReadLine());
- p1[i] = GetP1(p[i]);
- p2[i] = GetP2(p[i]);
- res[i] = (p[i] ^ p1[i]) & p2[i];
- }
- foreach (int answer in res)
- {
- Console.WriteLine(answer);
- }
- }
- private static int GetP2(int p)
- {
- int res = 0;
- string length = Convert.ToString(p, 2);
- for (int i = length.Length-1, j = 0; i >= 0; i--, j++)
- {
- if (((p >> i) & 1) == 1)
- {
- res = res | (1 << j);
- }
- }
- return res;
- }
- private static int GetP1(int p)
- {
- int res = 0;
- string length = Convert.ToString(p, 2);
- for (int i = 0; i < length.Length; i++)
- {
- if (((p >> i)&1) == 0)
- {
- res = res | (1 << i);
- }
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment