lmarkov

Exchange Of Bits Random Positions

Nov 30th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. /*
  2.  * * Write a program that exchanges bits {p, p+1, …, p+k-1) with bits {q, q+1, …, q+k-1} of given 32-bit unsigned integer.
  3. */
  4.  
  5. using System;
  6.  
  7. class ExchangeOfBitsRandomPositions
  8. {
  9.     static void Main()
  10.     {
  11.         uint number;
  12.         int i,j, p, q, k, jp, jq;
  13.         long valueBitPosition, valueBitPositionP, valueBitPositionQ;
  14.         int[] bitValue = new int[32];
  15.         int[] newBitValueP = new int[32];
  16.         int[] newBitValueQ = new int[32];
  17.  
  18.         Console.WriteLine("Enter number > 0: ");
  19.         while (!(uint.TryParse(Console.ReadLine(), out number)))
  20.         {
  21.             Console.WriteLine("Enter number > 0: ");
  22.         }
  23.         Console.WriteLine("Enter amount of bits you want to change k: ");
  24.         while (!(int.TryParse(Console.ReadLine(), out k)))
  25.         {
  26.             Console.WriteLine("Enter amount of bits you want to change k: ");
  27.         }
  28.         Console.WriteLine("Enter start position p: ");
  29.         while (!(int.TryParse(Console.ReadLine(), out p)))
  30.         {
  31.             Console.WriteLine("Enter start position p: ");
  32.         }
  33.         Console.WriteLine("Enter end position q: ");
  34.         while (!(int.TryParse(Console.ReadLine(), out q) && q > k+p))
  35.         {
  36.             Console.WriteLine("Enter end position q (q > k+p): ");
  37.         }
  38.        
  39.  
  40.         for (i = 0; i <= 31; i++)
  41.         {
  42.             valueBitPosition = ((1 << i) & number);
  43.             if (valueBitPosition == 0)
  44.             {
  45.                 bitValue[i] = 0;
  46.             }
  47.             else
  48.             {
  49.                 bitValue[i] = 1;
  50.             }            
  51.         }
  52.  
  53.         for (jp = p, jq = q, j = 0; j < k; jp++, jq++, j++)
  54.         {
  55.             valueBitPositionP = (((1 << jp) & number)) >> jp;
  56.             valueBitPositionQ = (((1 << jq) & number)) >> jq;
  57.  
  58.             if (valueBitPositionP != valueBitPositionQ)
  59.             {
  60.                 newBitValueP[jp] = bitValue[jq];
  61.                 newBitValueQ[jq] = bitValue[jp];
  62.  
  63.                 bitValue[jp] = newBitValueP[jp];
  64.                 bitValue[jq] = newBitValueQ[jq];
  65.             }
  66.         }
  67.         Array.Reverse(bitValue);
  68.        
  69.         Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0'));
  70.         //Console.WriteLine(bitValue.Length);        
  71.         for (j = 0; j <= 31; j++)
  72.         {
  73.             Console.Write(bitValue[j]);
  74.         }
  75.         Console.WriteLine("\n");
  76.         Main();
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment