Advertisement
Guest User

Change Positions

a guest
Mar 12th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 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 Change_on_specific_possitions
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int position;
  14. int 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 = int.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. int numOfSwaps = int.Parse(strNumofSwaps);
  38.  
  39. int counterNumSwaps = 1;
  40. int finalResult = number;
  41. int result;
  42. int resultChng;
  43. result = number;
  44. resultChng = number;
  45.  
  46.  
  47. while (counterNumSwaps <= numOfSwaps)
  48. {
  49. i = i << (position - 1);
  50. result = (result & i);
  51. int chngZeroOne;
  52. if (result > 0)
  53. chngZeroOne = 1;
  54. else
  55. chngZeroOne = 0;
  56.  
  57.  
  58. iChng = iChng << (positionChng - 1);
  59. resultChng = (resultChng & iChng);
  60. int chngZeroOneNew;
  61. if (resultChng > 0)
  62. chngZeroOneNew = 1;
  63. else
  64. chngZeroOneNew = 0;
  65.  
  66.  
  67.  
  68. if (chngZeroOne != chngZeroOneNew)
  69. {
  70.  
  71. finalResult = ((finalResult ^ i) ^ iChng);
  72.  
  73. }
  74. i = 1;
  75. position++;
  76. result = number;
  77. iChng = 1;
  78. positionChng++;
  79. resultChng = number;
  80.  
  81. counterNumSwaps++;
  82.  
  83. Console.WriteLine("The result after the swap is {0}", finalResult);
  84. }
  85. Console.WriteLine("The result after the swaps is {0}", finalResult);
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement