Advertisement
Guest User

Untitled

a guest
Sep 27th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System;
  2.  
  3. class Bit_Roller_2
  4. {
  5.     static void Main()
  6.     {
  7.         int size = 19;
  8.         int n = int.Parse(Console.ReadLine());                  //unsigned integer (in the range [0…524287]).
  9.         int pillarPosition = int.Parse(Console.ReadLine());     //integer in the range [0…18].
  10.         int rolls = int.Parse(Console.ReadLine());              //integer in the range [0…100].
  11.  
  12.         for (int i = 0; i < rolls; i++)
  13.         {
  14.             // roll
  15.             n = RollBitsRight(size, n);
  16.  
  17.             // exchange pillarPosition with the right one
  18.             if (pillarPosition == 0)
  19.                 n = ExchangeBits(n, pillarPosition, size - 1);
  20.             else
  21.                 n = ExchangeBits(n, pillarPosition, pillarPosition - 1);
  22.         }
  23.         Console.WriteLine(n);
  24.     }
  25.  
  26.     private static int ExchangeBits(int n, int firstPosition, int secondPosition)
  27.     {
  28.         int second = (n >> secondPosition) & 1;
  29.         int first = (n >> firstPosition) & 1;
  30.  
  31.  
  32.         n = n & ~(second << secondPosition);  //place 0 to secondPosition
  33.         n = n | first << secondPosition;    //copy first to secondPosition
  34.  
  35.         n = n & ~(first << firstPosition) | second << firstPosition; // the same as above in firstPosition
  36.         return n;
  37.     }
  38.  
  39.     private static int RollBitsRight(int size, int n)
  40.     {
  41.         int lastDigit = n & 1;
  42.         n >>= 1;
  43.         if (lastDigit == 1)
  44.             n |= 1 << (size - 1);
  45.         else //lastStr1 == 0
  46.             n &= ~(1 << (size - 1));
  47.         return n;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement