Advertisement
Guest User

BitsExchange

a guest
Mar 18th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. class Program
  2.     {
  3.         static uint number = uint.Parse(Console.ReadLine());
  4.         static uint unit = 1;
  5.         static void Main()
  6.         {
  7.             //first group
  8.             int bit3 = getBit(number, 3);
  9.             int bit4 = getBit(number, 4);
  10.             int bit5 = getBit(number, 5);
  11.             //second group
  12.             int bit24 = getBit(number, 24);
  13.             int bit25 = getBit(number, 25);
  14.             int bit26 = getBit(number, 26);
  15.             //Exchanges positions
  16.             putBitOnPosition(bit3, 24);
  17.             putBitOnPosition(bit4, 25);
  18.             putBitOnPosition(bit5, 26);
  19.             putBitOnPosition(bit24, 3);
  20.             putBitOnPosition(bit25, 4);
  21.             putBitOnPosition(bit26, 5);
  22.             Console.WriteLine(number);
  23.  
  24.         }
  25.        
  26.         static int getBit(uint number, int position)
  27.         {
  28.             int bit = (int) (number >> position) & 1;
  29.             return bit;
  30.         }
  31.  
  32.         static void putBitOnPosition(int bit, int position)
  33.         {
  34.             uint mask = unit << position;
  35.  
  36.             if ((number >> position & unit) != bit)
  37.             {
  38.                 number ^= mask;
  39.             }
  40.         }
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement