Advertisement
sylviapsh

We All Love Bits

Dec 27th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System;
  2. class WeAllLoveBits
  3. {
  4.   static void Main()
  5.   {
  6.     //Telerik Academy
  7.     //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̈.
  8.     //Your task is to write a program that transforms a sequence of N positive integer numbers using Mitko's algorithm.
  9.  
  10.     int n = int.Parse(Console.ReadLine());
  11.     uint newP = 0;
  12.     uint revP = 0;
  13.  
  14.     for (int counter = 1; counter <= n; counter++)
  15.     {
  16.       uint numP = uint.Parse(Console.ReadLine());
  17.      
  18.       while (numP > 0)
  19.       {
  20.         revP <<= 1;
  21.         if ((numP & 1) == 1)
  22.         {
  23.           revP |= 1;
  24.         }
  25.         numP >>= 1;
  26.    
  27.       }
  28.  
  29.       newP = numP ^ (~numP) & revP;
  30.       Console.WriteLine(newP);
  31.       revP = 0;
  32.     }
  33.   }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement