Advertisement
simeon_stoykov

Problem 5 - Bit Swapper

Nov 10th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class Problem5
  8. {
  9.     static void Main()
  10.     {
  11.         uint[] numbers = new uint[4];
  12.  
  13.         for (int i = 0; i < numbers.Length; i++)
  14.         {
  15.             numbers[i] = uint.Parse(Console.ReadLine());
  16.         }
  17.        
  18.         while (true)
  19.         {
  20.             string command1 = Console.ReadLine();
  21.            
  22.  
  23.             if (command1 == "End")
  24.             {
  25.                 break;
  26.             }
  27.  
  28.             string command2 = Console.ReadLine();
  29.  
  30.             string[] commands1 = command1.Split();
  31.             string[] commands2 = command2.Split();
  32.  
  33.             int firstNumberToSwap = int.Parse(commands1[0].ToString());
  34.             int numberToChangeFirst = firstNumberToSwap;
  35.             int firstPartOfBitsToSwap = int.Parse(commands1[1].ToString());
  36.             int secondNumberToSwap = int.Parse(commands2[0].ToString());
  37.             int numberToChangeSecond = secondNumberToSwap;
  38.             int secondPartOfBitsToSwap = int.Parse(commands2[1].ToString());
  39.  
  40.             if (firstNumberToSwap != secondNumberToSwap)
  41.             {
  42.                 uint firstToSwap = (numbers[firstNumberToSwap] & ((uint)15 << (firstPartOfBitsToSwap * 4))) >> (firstPartOfBitsToSwap * 4);
  43.                 uint secondToSwap = (numbers[secondNumberToSwap] & ((uint)15 << (secondPartOfBitsToSwap * 4))) >> (secondPartOfBitsToSwap * 4);
  44.  
  45.                 uint newFirstNumber = numbers[firstNumberToSwap] & ~((uint)15 << (firstPartOfBitsToSwap * 4));
  46.                 newFirstNumber = newFirstNumber | (secondToSwap << (firstPartOfBitsToSwap * 4));
  47.  
  48.                 uint newSecondNumber = numbers[secondNumberToSwap] & ~((uint)15 << (secondPartOfBitsToSwap * 4));
  49.                 newSecondNumber = newSecondNumber | (firstToSwap << (secondPartOfBitsToSwap * 4));
  50.  
  51.                 numbers[numberToChangeFirst] = newFirstNumber;
  52.                 numbers[numberToChangeSecond] = newSecondNumber;
  53.             }
  54.             else
  55.             {
  56.                 uint firstToSwap = (numbers[firstNumberToSwap] & ((uint)15 << (firstPartOfBitsToSwap * 4))) >> (firstPartOfBitsToSwap * 4);
  57.                 uint secondToSwap = (numbers[secondNumberToSwap] & ((uint)15 << (secondPartOfBitsToSwap * 4))) >> (secondPartOfBitsToSwap * 4);
  58.  
  59.                 uint newFirstNumber = numbers[firstNumberToSwap] & ~((uint)15 << (firstPartOfBitsToSwap * 4));
  60.                 newFirstNumber = newFirstNumber & ~((uint)15 << (secondPartOfBitsToSwap * 4));
  61.  
  62.                 newFirstNumber = newFirstNumber | (firstToSwap << (secondPartOfBitsToSwap * 4));
  63.                 newFirstNumber = newFirstNumber | (secondToSwap << (firstPartOfBitsToSwap * 4));
  64.                 numbers[numberToChangeFirst] = newFirstNumber;              
  65.             }          
  66.        }
  67.  
  68.         foreach (var item in numbers)
  69.         {
  70.             Console.WriteLine(item);
  71.         }        
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement