fbinnzhivko

05. Bitwise Operators

Mar 20th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.65 KB | None | 0 0
  1. using System;
  2. class Problem_5_We_All_Love_Bits
  3. {
  4.     static void Main()
  5.     {
  6.         // Read N
  7.         int N = int.Parse(Console.ReadLine());
  8.  
  9.         // For all N numbers
  10.         for (int i = 1; i <= N; i++)
  11.         {
  12.             // Read P
  13.             int P = int.Parse(Console.ReadLine());
  14.  
  15.             // Solve
  16.             int Pnew = 0;
  17.             while (P > 0)
  18.             {
  19.                 Pnew <<= 1;
  20.                 if ((P & 1) == 1)
  21.                 {
  22.                     Pnew |= 1;
  23.                 }
  24.                 P >>= 1;
  25.             }
  26.  
  27.             // Write Pnew
  28.             Console.WriteLine(Pnew);
  29.         }
  30.     }
  31. }
Add Comment
Please, Sign In to add comment