Advertisement
dimipan80

Exam 6. Bit Roller (String Solution)

Jun 30th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. namespace _5.BitRoller
  2. {
  3.     using System;
  4.  
  5.     public class BitRoller
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             checked
  10.             {
  11.                 uint num = uint.Parse(Console.ReadLine());
  12.                 int frozenBit = int.Parse(Console.ReadLine());
  13.                 int timesR = int.Parse(Console.ReadLine());
  14.  
  15.                 if (num != 0 && num != 524287)
  16.                 {
  17.                     string inputBinaryStr = Convert.ToString(num, 2);
  18.                     inputBinaryStr = inputBinaryStr.PadLeft(19, '0');
  19.                    
  20.                     int indexFrozenBit = 18 - frozenBit;
  21.                     string valueFrozenBit = inputBinaryStr[indexFrozenBit].ToString();
  22.  
  23.                     string binNumStr = string.Empty;
  24.                     if (indexFrozenBit > 0)
  25.                     {
  26.                         binNumStr += inputBinaryStr.Substring(0, indexFrozenBit);
  27.                     }
  28.  
  29.                     if (indexFrozenBit < 18)
  30.                     {
  31.                         binNumStr += inputBinaryStr.Substring(indexFrozenBit + 1);
  32.                     }
  33.  
  34.                     for (int i = 0; i < timesR; i++)
  35.                     {
  36.                         binNumStr = binNumStr[17].ToString() + binNumStr.Remove(17);
  37.                     }
  38.  
  39.                     string outputBinaryStr = binNumStr.Insert(indexFrozenBit, valueFrozenBit);
  40.          
  41.                     uint ouputNum = Convert.ToUInt32(outputBinaryStr, 2);
  42.                     Console.WriteLine(ouputNum);
  43.                 }
  44.                 else
  45.                 {
  46.                     Console.WriteLine(num);
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement