Advertisement
Guest User

Untitled

a guest
Sep 26th, 2015
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. class Bit_Roller
  3. { // rolls r times a 19-bit number n with a frozen bit at position f.
  4.     static void Main()
  5.     {
  6.         int size = 19;
  7.         int n = int.Parse(Console.ReadLine());
  8.         int pillarPosition = int.Parse(Console.ReadLine());
  9.         int rolls = int.Parse(Console.ReadLine());
  10.         int lastStr1;
  11.         int lastStr2;
  12.  
  13.         //get pillar
  14.         int pillar = (n >> pillarPosition) & 1;
  15.  
  16.         //get str2 {after pillar}
  17.         int mask = (1 << (pillarPosition)) - 1;            // mask = (1 << X) - 1 sets mask with '1' at last X positions
  18.         int str2 = n & mask;
  19.  
  20.         //get str1 {before pillar}
  21.         int str1 = n >> pillarPosition + 1;
  22.  
  23.         //lets roll
  24.         for (int i = 0; i < rolls; i++)
  25.         {
  26.             //get lastStr1 and lastStr2
  27.             lastStr1 = str1 & 1;
  28.             lastStr2 = str2 & 1;
  29.  
  30.             //roll str2 and place lastStr1 in front
  31.             str2 >>= 1;
  32.             if (lastStr1 == 1)
  33.                 str2 |= 1 << (pillarPosition - 1);
  34.             else //lastStr1 == 0
  35.                 str2 &= ~(1 << (pillarPosition - 1));
  36.  
  37.             //roll str1 and place lastStr2 in front
  38.             str1 >>= 1;
  39.             if (lastStr2 == 1)
  40.                 str1 |= 1 << (size - pillarPosition - 2);
  41.             else //lastStr2 == 0
  42.                 str1 &= ~(1 << (size - pillarPosition - 2));
  43.  
  44.         }
  45.         string bin = Convert.ToString(str1, 2).PadLeft(size - pillarPosition - 1, '0')
  46.             + pillar
  47.             + Convert.ToString(str2, 2).PadLeft(pillarPosition, '0');
  48.         //Console.WriteLine(bin);
  49.         Console.WriteLine(Convert.ToInt64(bin, 2));
  50.     }
  51. }
  52.  
  53. //Example: we have the number n = 2521, which is 0000000100111011001 in binary (as a 19-bit number). We freeze the bit at position f = 8 (we count the positions from the right, starting from 0). We roll out the number r = 4 times. We obtain the result 295245 as follows:
  54. //•   2521(10) = 0000000100111011001 - 1000000010101101100 (roll right with frozen position 8)
  55. //•   1000000010101101100 - 0100000001100110110 (roll right with frozen position 8)
  56. //•   0100000001100110110 - 0010000000110011011 (roll right with frozen position 8)
  57. //•   0010000000110011011 - 1001000000101001101 = 295245(10) (roll right with frozen position 8)
Advertisement
RAW Paste Data Copied
Advertisement