Advertisement
Masovski

[C# Basics][HW 3] 16. ExchangeBitsAdvanced

Mar 18th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. /* Description:
  2.  * Write a program that exchanges bits {p, p+1, …, p+k-1} with bits {q, q+1, …, q+k-1} of a given 32-bit unsigned integer.
  3.  * The first and the second sequence of bits may not overlap.
  4.  */
  5. using System;
  6.  
  7. class BitExchangeAdvanced
  8. {
  9.     static void Main()
  10.     {
  11.         while (true)
  12.         {
  13.  
  14.             Console.Write("Number: ");
  15.             uint number = 0;
  16.             bool isNumberValid = uint.TryParse(Console.ReadLine(), out number);
  17.             if (!isNumberValid)
  18.             {
  19.                 number = 0;
  20.             }
  21.                 Console.Write("p: ");
  22.                 int p = int.Parse(Console.ReadLine());
  23.                 Console.Write("q: ");
  24.                 int q = int.Parse(Console.ReadLine());
  25.                 Console.Write("k: ");
  26.                 int k = int.Parse(Console.ReadLine());
  27.                 k--;
  28.  
  29.                 bool isOutOfRange = (p + k) > 31 || (q + k) > 31;
  30.                 bool isOverlapping = (p + q) <= k;
  31.      
  32.             for (int i = 0; i <= k; i++)
  33.             {
  34.                 // Getting the positions
  35.                 int tempP = p + i;
  36.                 int tempQ = q + i;
  37.  
  38.                 // Getting the first bit
  39.                 uint tempBit = number >> tempP;
  40.                 tempBit = tempBit & 1;
  41.  
  42.                 // Getting the second bit
  43.                 uint secondTempBit = number >> tempQ;
  44.                 secondTempBit = secondTempBit & 1;
  45.  
  46.                 // Placing the first bit into the position of the second
  47.                 if (tempBit == 0)
  48.                 {
  49.                     int mask = ~(1 << tempQ);
  50.                     number = number & (uint)mask;
  51.                 }
  52.                 else if (tempBit == 1)
  53.                 {
  54.                     int mask = 1 << tempQ;
  55.                     number = number | (uint)mask;
  56.  
  57.                 }
  58.  
  59.                 // Placing the first bit into the position of the second
  60.                 if (secondTempBit == 0)
  61.                 {
  62.                     int mask = ~(1 << tempP);
  63.                     number = number & (uint)mask;
  64.                 }
  65.                 else if (secondTempBit == 1)
  66.                 {
  67.                     int mask = 1 << tempP;
  68.                     number = number | (uint)mask;
  69.  
  70.                 }
  71.            
  72.             }
  73.             if (isOverlapping && !isOutOfRange)
  74.             {
  75.                 Console.Write("Result: ");
  76.                 Console.ForegroundColor = ConsoleColor.Red;
  77.                 Console.WriteLine("Overlapping!");
  78.                 Console.ResetColor();
  79.             }
  80.             else if (isOutOfRange)
  81.             {
  82.                 Console.Write("Result: ");
  83.                 Console.ForegroundColor = ConsoleColor.Red;
  84.                 Console.WriteLine("Out of range!");
  85.                 Console.ResetColor();
  86.             }
  87.             else
  88.             {
  89.                 Console.Write("Result: ");
  90.                 Console.ForegroundColor = ConsoleColor.Green;
  91.                 Console.WriteLine(number);
  92.                 Console.ResetColor();
  93.             }
  94.             Console.WriteLine();
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement