psotirov

We all Love Bits!

Dec 10th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2.  
  3. class LoveBits
  4. {
  5.     static void Main()
  6.     {
  7.         int N = int.Parse(Console.ReadLine());
  8.  
  9.         int[] result = new int[N];
  10.         for (int i = 0; i < N; i++)
  11.         {
  12.             int P = int.Parse(Console.ReadLine());
  13.             int bitLength = 32; // The binary length of the number is 32 bits initially
  14.             while ((P >> (bitLength - 1) & 1) == 0) bitLength--;
  15.             // checks each bit from the most to the least one and stops on first 1 - this is the exact length of the binary digit
  16.  
  17.             // Makes magic operation - Pnew = = (P ^ Pinversed) & Preversed
  18.             // Since (P ^ Pinversed) always is 11111..1111, and 1111.11 & number = number
  19.             // 110011 XOR 001100 = 111111, and 111111 AND 110011 = 110011
  20.             // Pnew = Preversed
  21.             for (int j = 0; j < bitLength; j++)
  22.                 result[i] = (result[i] << 1) | ((P >> j) & 1);
  23.                 //taking each bit from number P (from right to left) and put it into Pnew (from left to right)
  24.         }
  25.         for (int i = 0; i < N; i++) // Finally prints the result
  26.         {
  27.             Console.WriteLine(result[i]);            
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment