Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class WeAllLoveBits
- {
- static void Main()
- {
- //Telerik Academy
- //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̈.
- //Your task is to write a program that transforms a sequence of N positive integer numbers using Mitko's algorithm.
- int n = int.Parse(Console.ReadLine());
- uint newP = 0;
- uint revP = 0;
- for (int counter = 1; counter <= n; counter++)
- {
- uint numP = uint.Parse(Console.ReadLine());
- while (numP > 0)
- {
- revP <<= 1;
- if ((numP & 1) == 1)
- {
- revP |= 1;
- }
- numP >>= 1;
- }
- newP = numP ^ (~numP) & revP;
- Console.WriteLine(newP);
- revP = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement