vonko1988

C#_ExchangeBitsOnPos(p,...p+k-1)WithBitsOnPos(q,..q+k-1)

Mar 4th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2.  
  3. class ChangeBitsP_Q_K
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Write the number n:");
  8.         uint n = Convert.ToUInt32(Console.ReadLine());
  9.         string binary = Convert.ToString(n, 2);
  10.  
  11.         Console.WriteLine("Write the first starting position p:");
  12.         byte p = Convert.ToByte(Console.ReadLine());
  13.         Console.WriteLine("Write the second starting position q:");
  14.         byte q = Convert.ToByte(Console.ReadLine());
  15.         Console.WriteLine("Write the length of the two sequences of bits k:");
  16.         byte k = Convert.ToByte(Console.ReadLine());
  17.  
  18.         Console.WriteLine("The number in binary is: {0}", binary);
  19.        
  20.         uint mask = 1;
  21.  
  22.         //Here we create the mask of 1s
  23.         for (int i = 0; i < k; i++)
  24.         {
  25.             mask *= 2;
  26.         }
  27.  
  28.         uint juniorChecker = (mask - 1) << p;
  29.         string juniorCheckerStr = Convert.ToString(juniorChecker, 2);
  30.         Console.WriteLine("The junior checker is: {0}", juniorCheckerStr);
  31.         Console.WriteLine();
  32.         //Now we wiil get the junior bits
  33.         uint juniorBits = n & juniorChecker;
  34.         string juniorBitsStr = Convert.ToString(juniorBits, 2);
  35.         Console.WriteLine("The junior bits are: {0}", juniorBitsStr);
  36.         Console.WriteLine();
  37.         //Now we will null the junior bits
  38.         n = n & (~juniorBits);
  39.         string binary2 = Convert.ToString(n, 2);
  40.         Console.WriteLine("The number with junior positions nulled is: {0}", binary2);
  41.         Console.WriteLine();
  42.         //Now we will move the senior bits to the position of the junior bits
  43.         juniorBits = juniorBits << (q - p);
  44.         string juniorBitsPos = Convert.ToString(juniorBits, 2);
  45.         Console.WriteLine("The mask with junior bits on their new positions is: {0}", juniorBitsPos);
  46.         Console.WriteLine();
  47.  
  48.         uint seniorChecker = (mask - 1) << q;
  49.         string seniorCheckerStr = Convert.ToString(seniorChecker, 2);
  50.         Console.WriteLine("The senior checker is: {0}", seniorCheckerStr);
  51.         Console.WriteLine();
  52.         //Now we will get the senior bits
  53.         uint seniorBits = n & seniorChecker;
  54.         string seniorBitsStr = Convert.ToString(seniorBits, 2);
  55.         Console.WriteLine("The senior bits are: {0}", seniorBitsStr);
  56.         Console.WriteLine();
  57.         //Now we will null the senior bits
  58.         n = n & (~seniorBits);
  59.         string binary3 = Convert.ToString(n, 2);
  60.         Console.WriteLine("The number with the junior and senior bits nulled is: {0}", binary3);
  61.         Console.WriteLine();
  62.         //Now we will move the senior bits to the position of the junior bits
  63.         seniorBits = seniorBits >> (q - p);
  64.         string seniorBitsPos = Convert.ToString(seniorBits, 2);
  65.         Console.WriteLine("The mask with the senior bits on their new position is: {0}", seniorBitsPos);
  66.         Console.WriteLine();
  67.  
  68.         //Now we will position the junior bits and senior bits on their new position
  69.         n = n | seniorBits;
  70.         n = n | juniorBits;
  71.         string nFinal = Convert.ToString(n, 2);
  72.         Console.WriteLine("The number with the bits exchanged is: {0}", nFinal);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment