Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _16a.Bit_Exchange__Advanced_
- {
- class Program
- {
- static void Main(string[] args)
- {
- int position;
- uint number;
- string strNum;
- string strPos;
- int i = 1;
- Console.WriteLine("Please enter initial number ");
- strNum = Console.ReadLine();
- Console.WriteLine("Please enter 1st position where you want to change ");
- strPos = Console.ReadLine();
- position = int.Parse(strPos);
- number = uint.Parse(strNum);
- int positionChng;
- string strPosChng;
- int iChng = 1;
- Console.WriteLine("Please enter 1st position of the numbers you will change with ");
- strPosChng = Console.ReadLine();
- positionChng = int.Parse(strPosChng);
- Console.WriteLine("Please enter the number of exchanges ");
- string strNumofSwaps = Console.ReadLine();
- uint numOfSwaps = uint.Parse(strNumofSwaps);
- if ((((Math.Min(position, positionChng) + numOfSwaps) >= Math.Max(position, positionChng)) || (position + numOfSwaps) > 32 || ((positionChng + numOfSwaps) > 32)))
- {
- Console.WriteLine("The positions and number of swaps you entered caused overlap or make scope out of range");
- }
- else
- {
- uint counterNumSwaps = 1;
- uint finalResult = number;
- uint result;
- uint resultChng;
- result = number;
- resultChng = number;
- while (counterNumSwaps <= numOfSwaps)
- {
- i = i << position;
- result = (result & (uint)i);
- int chngZeroOne;
- if (result > 0)
- chngZeroOne = 1;
- else
- chngZeroOne = 0;
- iChng = iChng << positionChng;
- resultChng = (resultChng & (uint)iChng);
- int chngZeroOneNew;
- if (resultChng > 0)
- chngZeroOneNew = 1;
- else
- chngZeroOneNew = 0;
- if (chngZeroOne != chngZeroOneNew)
- {
- finalResult = ((finalResult ^ (uint)i) ^ (uint)iChng);
- }
- i = 1;
- position++;
- result = number;
- iChng = 1;
- positionChng++;
- resultChng = number;
- counterNumSwaps++;
- Console.WriteLine("The result after the respective swap is {0}", finalResult);
- }
- Console.WriteLine("The final result after the swaps is {0}", finalResult);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement