Venciity

[SoftUni C# exam preparation] 05.WeAllLoveBits [decision 2]

Apr 10th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class WeAllLoveBits2
  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.             Console.WriteLine("start : " + Convert.ToString(num, 2));
  17.  
  18.             result = 0; // reset for every number
  19.             while (num > 0)
  20.             {
  21.                 int lastBit = num & 1; // get last bit
  22.                 num = num >> 1;
  23.                 result = result << 1; //releasing the last position
  24.                 result = result | lastBit;
  25.                 Console.WriteLine("num : " + Convert.ToString(num, 2));
  26.                 Console.WriteLine("result : " + Convert.ToString(result, 2));
  27.                 Console.ReadLine();
  28.             }
  29.             Console.WriteLine(result);
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment