Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using System;
  2. class BitsExchange
  3. {
  4.     static void Main()
  5.     {
  6.         Console.WriteLine("Please enter the number");
  7.         uint number = uint.Parse(Console.ReadLine());
  8.         Console.WriteLine("Number in binary");
  9.         Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0'));
  10.  
  11.         int firstBitsGroupPosition = 3;//position 3,4,5
  12.         int secondBitsGroupPosition = 24;//position 24,25,26
  13.  
  14.         uint firstBitsGroupPositionMask = 7u << firstBitsGroupPosition;
  15.         uint secondBitsGroupPositionMask = 7u << secondBitsGroupPosition;
  16.  
  17.         uint getFirstGroupBits = firstBitsGroupPositionMask & number;
  18.         uint getSecondGroupBits = secondBitsGroupPositionMask & number;
  19.  
  20.         uint newMask = (getFirstGroupBits >> firstBitsGroupPosition) ^ (getSecondGroupBits >> secondBitsGroupPosition);/* we use the ^ to create new mask and use to excange bits */
  21.  
  22.         uint newNumber = ((newMask << firstBitsGroupPosition) ^ number);
  23.         newNumber = ((newMask << secondBitsGroupPosition) ^ newNumber);
  24.         Console.WriteLine(Convert.ToString(newNumber, 2).PadLeft(32, '0'));
  25.         Console.WriteLine(newNumber);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement