Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Bit_Roller
- { // rolls r times a 19-bit number n with a frozen bit at position f.
- static void Main()
- {
- int size = 19;
- int n = int.Parse(Console.ReadLine());
- int pillarPosition = int.Parse(Console.ReadLine());
- int rolls = int.Parse(Console.ReadLine());
- int lastStr1;
- int lastStr2;
- //get pillar
- int pillar = (n >> pillarPosition) & 1;
- //get str2 {after pillar}
- int mask = (1 << (pillarPosition)) - 1; // mask = (1 << X) - 1 sets mask with '1' at last X positions
- int str2 = n & mask;
- //get str1 {before pillar}
- int str1 = n >> pillarPosition + 1;
- //lets roll
- for (int i = 0; i < rolls; i++)
- {
- //get lastStr1 and lastStr2
- lastStr1 = str1 & 1;
- lastStr2 = str2 & 1;
- //roll str2 and place lastStr1 in front
- str2 >>= 1;
- if (lastStr1 == 1)
- str2 |= 1 << (pillarPosition - 1);
- else //lastStr1 == 0
- str2 &= ~(1 << (pillarPosition - 1));
- //roll str1 and place lastStr2 in front
- str1 >>= 1;
- if (lastStr2 == 1)
- str1 |= 1 << (size - pillarPosition - 2);
- else //lastStr2 == 0
- str1 &= ~(1 << (size - pillarPosition - 2));
- }
- string bin = Convert.ToString(str1, 2).PadLeft(size - pillarPosition - 1, '0')
- + pillar
- + Convert.ToString(str2, 2).PadLeft(pillarPosition, '0');
- //Console.WriteLine(bin);
- Console.WriteLine(Convert.ToInt64(bin, 2));
- }
- }
- //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:
- //• 2521(10) = 0000000100111011001 - 1000000010101101100 (roll right with frozen position 8)
- //• 1000000010101101100 - 0100000001100110110 (roll right with frozen position 8)
- //• 0100000001100110110 - 0010000000110011011 (roll right with frozen position 8)
- //• 0010000000110011011 - 1001000000101001101 = 295245(10) (roll right with frozen position 8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement