lmarkov

Exchange Of Bits

Nov 29th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. /*
  2.  * Write a program that exchanges bits 3, 4 and 5 with bits 24, 25 and 26 of given 32-bit unsigned integer.
  3. */
  4.  
  5. using System;
  6.  
  7. class ExchangeOfBits
  8. {
  9.     static void Main()
  10.     {
  11.         uint number;
  12.         int i,j;
  13.         long valueBitPosition;
  14.         int[] bitValue = new int[32];
  15.         int[] newBitValue = new int[32];
  16.  
  17.         Console.WriteLine("Enter number > 0: ");
  18.         while(!(uint.TryParse(Console.ReadLine(), out number)))
  19.         {
  20.             Console.WriteLine("Enter number > 0: ");    
  21.         }
  22.         for (i = 0; i <= 31; i++)
  23.         {
  24.             valueBitPosition = ((1 << i) & number);
  25.             if (valueBitPosition == 0)
  26.             {
  27.                 bitValue[i] = 0;
  28.             }
  29.             else
  30.             {
  31.                 bitValue[i] = 1;
  32.             }
  33.         }
  34.         newBitValue[3] = bitValue[24];
  35.         newBitValue[4] = bitValue[25];
  36.         newBitValue[5] = bitValue[26];
  37.         newBitValue[24] = bitValue[3];
  38.         newBitValue[25] = bitValue[4];
  39.         newBitValue[26] = bitValue[5];
  40.  
  41.         bitValue[3] = newBitValue[3];
  42.         bitValue[4] = newBitValue[4];
  43.         bitValue[5] = newBitValue[5];
  44.         bitValue[24] = newBitValue[24];
  45.         bitValue[25] = newBitValue[25];
  46.         bitValue[26] = newBitValue[26];
  47.  
  48.         Array.Reverse(bitValue);        
  49.      
  50.         Console.WriteLine(Convert.ToString(number,2).PadLeft(32,'0'));
  51.         //Console.WriteLine(bitValue.Length);        
  52.         for (j = 0; j <= 31; j++)
  53.         {
  54.             Console.Write(bitValue[j]);
  55.         }
  56.         Console.WriteLine("\n");
  57.         Main();
  58.    
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment