Advertisement
Guest User

Problem 16

a guest
Mar 15th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _16a.Bit_Exchange__Advanced_
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int position;
  14.             uint number;
  15.             string strNum;
  16.             string strPos;
  17.             int i = 1;
  18.             Console.WriteLine("Please enter initial number ");
  19.             strNum = Console.ReadLine();
  20.             Console.WriteLine("Please enter 1st position where you want to change ");
  21.             strPos = Console.ReadLine();
  22.  
  23.             position = int.Parse(strPos);
  24.             number = uint.Parse(strNum);
  25.  
  26.             int positionChng;
  27.             string strPosChng;
  28.             int iChng = 1;
  29.  
  30.             Console.WriteLine("Please enter 1st position of the numbers you will change with ");
  31.             strPosChng = Console.ReadLine();
  32.             positionChng = int.Parse(strPosChng);
  33.  
  34.  
  35.             Console.WriteLine("Please enter the number of exchanges ");
  36.             string strNumofSwaps = Console.ReadLine();
  37.             uint numOfSwaps = uint.Parse(strNumofSwaps);
  38.             if ((((Math.Min(position, positionChng) + numOfSwaps) >= Math.Max(position, positionChng)) || (position + numOfSwaps) > 32 || ((positionChng + numOfSwaps) > 32)))
  39.             {
  40.                 Console.WriteLine("The positions and number of swaps you entered caused overlap or make scope out of range");
  41.             }
  42.  
  43.             else
  44.             {
  45.  
  46.                 uint counterNumSwaps = 1;
  47.                 uint finalResult = number;
  48.                 uint result;
  49.                 uint resultChng;
  50.                 result = number;
  51.                 resultChng = number;
  52.  
  53.  
  54.                 while (counterNumSwaps <= numOfSwaps)
  55.                 {
  56.                     i = i << position;
  57.                     result = (result & (uint)i);
  58.                     int chngZeroOne;
  59.                     if (result > 0)
  60.                         chngZeroOne = 1;
  61.                     else
  62.                         chngZeroOne = 0;
  63.  
  64.  
  65.                     iChng = iChng << positionChng;
  66.                     resultChng = (resultChng & (uint)iChng);
  67.                     int chngZeroOneNew;
  68.                     if (resultChng > 0)
  69.                         chngZeroOneNew = 1;
  70.                     else
  71.                         chngZeroOneNew = 0;
  72.  
  73.  
  74.  
  75.                     if (chngZeroOne != chngZeroOneNew)
  76.                     {
  77.  
  78.  
  79.                         finalResult = ((finalResult ^ (uint)i) ^ (uint)iChng);
  80.  
  81.                     }
  82.                     i = 1;
  83.                     position++;
  84.                     result = number;
  85.                     iChng = 1;
  86.                     positionChng++;
  87.                     resultChng = number;
  88.  
  89.                     counterNumSwaps++;
  90.  
  91.                     Console.WriteLine("The result after the respective swap is {0}", finalResult);
  92.                 }
  93.                 Console.WriteLine("The final result after the swaps is {0}", finalResult);
  94.             }
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement