Advertisement
funbun

Bits Exchange

Nov 17th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class BitsExchange
  5. {
  6.     static void Main()
  7.     {
  8.         UInt32 number = UInt32.Parse(Console.ReadLine());
  9.         Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0'));
  10.  
  11.         UInt32 oneDigitBit = 0;
  12.         UInt32 twoDigitBit = 0;
  13.  
  14.         for (byte i = 3, j = 24; i <= 5 || j <= 26; i++, j++)
  15.         {
  16.             UInt32 mask1 = 1;
  17.             mask1 = mask1 << i;
  18.             oneDigitBit = (mask1 & number) >> i;
  19.  
  20.             UInt32 mask2 = 1;
  21.             mask2 = mask2 << j;
  22.             twoDigitBit = (mask2 & number) >> j;
  23.  
  24.             if (oneDigitBit == twoDigitBit)
  25.             {
  26.                 continue;
  27.             }
  28.             else if (oneDigitBit == 0 && twoDigitBit == 1)
  29.             {
  30.                 number = mask1 | number;
  31.                 number = ~mask2 & number;
  32.             }
  33.             else if (oneDigitBit == 1 && twoDigitBit == 0)
  34.             {
  35.                 number = ~mask1 & number;
  36.                 number = mask2 | number;
  37.             }
  38.         }
  39.         Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0'));
  40.         Console.WriteLine(number);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement