Advertisement
nmnikolov

AdvancedBitExchange

Jun 19th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. using System;
  2.  
  3. //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.
  4. //The first and the second sequence of bits may not overlap. Examples:
  5. // |           n |  p |  q |  k |          binary representation of n |                       binary result |       result |
  6. // |  1140867093 |  3 | 24 |  3 | 01000100 00000000 01000000 00010101 | 01000010 00000000 01000000 00100101 |   1107312677 |
  7. // |  4294901775 | 24 |  3 |  3 | 11111111 11111111 00000000 00001111 | 11111001 11111111 00000000 00111111 |   4194238527 |
  8. // |  2369124121 |  2 | 22 | 10 | 10001101 00110101 11110111 00011001 | 01110001 10110101 11111000 11010001 |   1907751121 |
  9. // |   987654321 |  2 |  8 | 11 |                                   - |                                   - |  overlapping |
  10. // |   123456789 | 26 |  0 |  7 |                                   - |                                   - | out of range |
  11. // | 33333333333 | -1 |  0 | 33 |                                   - |                                   - | out of range |
  12.  
  13. class AdvancedBitExchange
  14. {
  15.     static void Main()
  16.     {
  17.         uint n = 0;
  18.         try
  19.         {
  20.             Console.Write("Input n: ");
  21.             string number = Console.ReadLine();
  22.             n = uint.Parse(number);
  23.         }
  24.         catch (OverflowException)
  25.         {
  26.             Console.WriteLine("n is out of range");
  27.             return;
  28.         }        
  29.         Console.Write("Input p: ");
  30.         int p = int.Parse(Console.ReadLine());
  31.         Console.Write("Input q: ");
  32.         int q = int.Parse(Console.ReadLine());
  33.         Console.Write("Input k: ");
  34.         int k = int.Parse(Console.ReadLine());
  35.         bool overlapping = Math.Abs(p - q) < k ? true : false;
  36.         bool outOfRange = p < 0 | q < 0 | p + (k - 1) > 31 | q + (k - 1) > 31;
  37.  
  38.         if (outOfRange != true && overlapping != true)
  39.         {
  40.             uint firstBits = n << (32 - p - k);
  41.             firstBits = firstBits >> (32 - k);
  42.             uint secondBits = n << (32 - q - k);
  43.             secondBits = secondBits >> (32 - k);
  44.             n = n & ~(firstBits << p)  | secondBits << p;
  45.             n = n & ~(secondBits << q) | firstBits << q;
  46.             Console.WriteLine("Result: {0}", n);
  47.             Console.WriteLine("Binary result: {0}", Convert.ToString(n, 2).PadLeft(32, '0'));
  48.         }
  49.         else if (outOfRange == true)
  50.         {
  51.             Console.WriteLine("out of range");
  52.         }
  53.         else
  54.         {
  55.             Console.WriteLine("overlapping");
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement