Venciity

[SoftUni C# exam preparation] 05.WeAllLoveBits

Apr 10th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class WeAllLoveBits
  6. {
  7.     static void Main()
  8.     {
  9.         int n = Int32.Parse(Console.ReadLine());
  10.  
  11.         int result = 0;
  12.  
  13.         for (int i = 0; i < n; i++)
  14.         {
  15.             int num = Int32.Parse(Console.ReadLine());
  16.  
  17.             result = 0; // reset for every number
  18.             while (num > 0)
  19.             {
  20.                 int lastBit = num & 1; // get last bit
  21.                 num = num >> 1;
  22.                 result = result << 1; //releasing the last position
  23.                 result = result | lastBit;
  24.             }
  25.             Console.WriteLine(result);
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment