syssboxx

Exchange bit 3-4-5 with 24-25-26

May 29th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. //Write a program that exchanges bits 3, 4 and 5 with bits 24, 25 and 26 of given 32-bit unsigned integer.
  2.  
  3.  
  4. using System;
  5.  
  6. class ExchangeBits345With242526Of32Unit
  7. {
  8.     static void Main()
  9.     {
  10.         uint number;
  11.         uint actualBitValue3, actualBitValue4, actualBitValue5;
  12.         uint actualBitValue24, actualBitValue25, actualBitValue26;
  13.         uint modifiedNumber;
  14.        
  15.         Console.Write("Enter an integer number : ");
  16.         number = uint.Parse(Console.ReadLine());
  17.         Console.WriteLine("Binar representation :{0}", Convert.ToString(number, 2).PadLeft(32, '0'));
  18.              
  19.         //find the actual bit values at selected positions
  20.         actualBitValue3 = (number & (1 << 3))>>3;
  21.         actualBitValue4 = (number & (1 << 4))>>4;
  22.         actualBitValue5 = (number & (1 << 5)) >> 5;
  23.         actualBitValue24 = (number & (1 << 24)) >> 24;
  24.         actualBitValue25 = (number & (1 << 25)) >> 25;
  25.         actualBitValue26 = (number & (1 << 26)) >> 26;
  26.        
  27.         Console.WriteLine();
  28.  
  29.         //set the new values to the number
  30.        
  31.         //if the the actual bit is 1 we change it to 0, otherwise we change it to 1
  32.         if (actualBitValue3 == 0)
  33.         {
  34.             modifiedNumber = number & ~(uint)(1 << 24);
  35.         }
  36.         else
  37.         {
  38.             modifiedNumber = number | (1 << 24);
  39.         }
  40.            
  41.         if (actualBitValue4 == 0)
  42.         {
  43.             modifiedNumber = modifiedNumber & ~(uint)(1 << 25);
  44.         }
  45.         else
  46.         {
  47.             modifiedNumber = modifiedNumber | (1 << 25);
  48.         }
  49.        
  50.         if (actualBitValue5 == 0)
  51.         {
  52.             modifiedNumber = modifiedNumber & ~(uint)(1 << 26);
  53.         }
  54.         else
  55.         {
  56.             modifiedNumber = modifiedNumber | (1 << 26);
  57.         }
  58.        
  59.         if (actualBitValue24 == 0)
  60.         {
  61.             modifiedNumber = modifiedNumber & ~(uint)(1 << 3);
  62.         }
  63.         else
  64.         {
  65.             modifiedNumber = modifiedNumber | (1 << 3);
  66.         }
  67.        
  68.         if (actualBitValue25 == 0)
  69.         {
  70.             modifiedNumber = modifiedNumber & ~(uint)(1 << 4);
  71.         }
  72.         else
  73.         {
  74.             modifiedNumber = modifiedNumber | (1 << 4);
  75.         }
  76.        
  77.         if (actualBitValue26 == 0)
  78.         {
  79.             modifiedNumber = modifiedNumber & ~(uint)(1 << 5);
  80.         }
  81.         else
  82.         {
  83.             modifiedNumber = modifiedNumber | (1 << 5);
  84.         }
  85.        
  86.         Console.WriteLine("The modified number is {0} :{1}", modifiedNumber, Convert.ToString(modifiedNumber, 2).PadLeft(32, '0'));      
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment