svetlozar_kirkov

Bit Roller (100/100)

Nov 19th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace BitRoller
  5. {
  6.     class BitRoller
  7.     {
  8.         static void Main()
  9.         {
  10.             uint n = uint.Parse(Console.ReadLine());
  11.             uint f = uint.Parse(Console.ReadLine());
  12.             uint r = uint.Parse(Console.ReadLine());
  13.             List<int> binary = new List<int>();
  14.             string binaryNum = Convert.ToString(n, 2).PadLeft(19, '0');
  15.             for (int i = 0; i < binaryNum.Length; i++)
  16.             {
  17.                 binary.Add(Convert.ToInt32(binaryNum[i].ToString()));
  18.             }
  19.             int extractedBit = binary[(int) (18 - f)];
  20.             binary.RemoveAt((int)(18-f));
  21.             string missing = string.Join("", binary);
  22.             for (int i = 0; i < r; i++)
  23.             {
  24.                 missing = missing.PadLeft(19, missing[missing.Length - 1]);
  25.                 missing = missing.Remove(missing.Length - 1);
  26.             }
  27.             List<int> final = new List<int>();
  28.             for (int i = 0; i < missing.Length; i++)
  29.             {
  30.                 final.Add(Convert.ToInt32(missing[i].ToString()));
  31.             }
  32.             final.Insert((int)(18 - f), extractedBit);
  33.             string finale = string.Join("", final);
  34.             Console.WriteLine(Convert.ToUInt32(finale,2));
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment