SHOW:
|
|
- or go back to the newest paste.
1 | using System; | |
2 | ||
3 | class BitRoller | |
4 | { | |
5 | static void Main() | |
6 | { | |
7 | - | int n = int.Parse(Console.ReadLine()); |
7 | + | int n = int.Parse(Console.ReadLine()); |
8 | int f = int.Parse(Console.ReadLine()); | |
9 | int r = int.Parse(Console.ReadLine()); | |
10 | int frozenBit = (n & (1 << f)) == 0 ? 0 : 1; | |
11 | for (int i = 0; i < r; i++) | |
12 | { | |
13 | ||
14 | - | if ((n & 1) != 0) //add bit @ pos 19 == to bit @ pos 0 |
14 | + | if (((n & 1) != 0) && (f != 0)) //@ pos 19 adds 1 if value of bit @ pos is 1 and f !=0 |
15 | { | |
16 | n = n | (1 << 19); | |
17 | } | |
18 | int leftBit = (n & (1 << (f + 1))) == 0 ? 0 : 1; // the bit left from the frozenBit | |
19 | n = n & (~(1 << f)) | (leftBit << f); // exchange left bit and frozen bit | |
20 | n = n & (~(1 << (f+1))) | (frozenBit << (f+1)); | |
21 | n = n >> 1; // removes right most bit | |
22 | } | |
23 | ||
24 | Console.WriteLine(n); | |
25 | } | |
26 | } |