Advertisement
Guest User

[C# Basics Exam 12 April 2014 Evening] Bit Roller

a guest
Aug 20th, 2014
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. class BitRoller
  4. {
  5.     static void Main()
  6.     {
  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) && (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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement