Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Bit_Roller_2
- {
- static void Main()
- {
- int size = 19;
- int n = int.Parse(Console.ReadLine()); //unsigned integer (in the range [0…524287]).
- int pillarPosition = int.Parse(Console.ReadLine()); //integer in the range [0…18].
- int rolls = int.Parse(Console.ReadLine()); //integer in the range [0…100].
- for (int i = 0; i < rolls; i++)
- {
- // roll
- n = RollBitsRight(size, n);
- // exchange pillarPosition with the right one
- if (pillarPosition == 0)
- n = ExchangeBits(n, pillarPosition, size - 1);
- else
- n = ExchangeBits(n, pillarPosition, pillarPosition - 1);
- }
- Console.WriteLine(n);
- }
- private static int ExchangeBits(int n, int firstPosition, int secondPosition)
- {
- int second = (n >> secondPosition) & 1;
- int first = (n >> firstPosition) & 1;
- n = n & ~(second << secondPosition); //place 0 to secondPosition
- n = n | first << secondPosition; //copy first to secondPosition
- n = n & ~(first << firstPosition) | second << firstPosition; // the same as above in firstPosition
- return n;
- }
- private static int RollBitsRight(int size, int n)
- {
- int lastDigit = n & 1;
- n >>= 1;
- if (lastDigit == 1)
- n |= 1 << (size - 1);
- else //lastStr1 == 0
- n &= ~(1 << (size - 1));
- return n;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement