Advertisement
striking

Untitled

Jan 29th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class BitExchangeAdvanced
  5. {
  6. static void Main(string[] args)
  7. {
  8. string number = Console.ReadLine();
  9. uint p = uint.Parse(Console.ReadLine());
  10. uint q = uint.Parse(Console.ReadLine());
  11. uint kLength = uint.Parse(Console.ReadLine());
  12.  
  13. uint n = 0;
  14. bool outOfRange = true;
  15. uint closestBit = (p > q) ? q : p;
  16. uint longestBit = (p > q) ? p : q;
  17. outOfRange = uint.TryParse(number, out n);
  18.  
  19. if (outOfRange && (p >= 0) && (q >= 0) && (p + kLength <= 32) && (q + kLength <= 32) && (kLength <= 32))
  20. {
  21. n = uint.Parse(number);
  22. }
  23. else
  24. {
  25. Console.WriteLine("out of range");
  26. return;
  27.  
  28. }
  29.  
  30. bool overlapping = false;
  31. overlapping = (closestBit + kLength) > longestBit;
  32. if (overlapping)
  33. {
  34. Console.WriteLine("overlapping");
  35. return;
  36. }
  37.  
  38. List<string> numberInBits = new List<string>();
  39. string converedNumber = Convert.ToString(n, 2).PadLeft(32, '0');
  40. List<string> firstValueOfBits = new List<string>();
  41. List<string> secondValueOfBits = new List<string>();
  42.  
  43. for (int i = 0; i < converedNumber.Length; i++)
  44. {
  45. numberInBits.Add(converedNumber[i].ToString());
  46. }
  47. numberInBits.Reverse();
  48.  
  49. uint nRightBit = 0;
  50. uint bitValue = 0;
  51. int OneMorePositionBack = 0;
  52.  
  53. for (uint i = closestBit; i < closestBit + kLength; i++)
  54. {
  55. nRightBit = n >> (int)closestBit + OneMorePositionBack;
  56. bitValue = nRightBit & 1;
  57. firstValueOfBits.Add(bitValue.ToString());
  58. OneMorePositionBack++;
  59. }
  60.  
  61. OneMorePositionBack = 0;
  62. for (uint i = longestBit; i < longestBit + kLength; i++)
  63. {
  64. nRightBit = n >> (int)longestBit + OneMorePositionBack;
  65. bitValue = nRightBit & 1;
  66. secondValueOfBits.Add(bitValue.ToString());
  67. OneMorePositionBack++;
  68. }
  69.  
  70. int countingPosition = 0;
  71. for (uint i = closestBit; i < closestBit + kLength; i++)
  72. {
  73. numberInBits.RemoveAt((int)i);
  74. numberInBits.Insert((int)i, secondValueOfBits[countingPosition]);
  75. countingPosition++;
  76. }
  77. countingPosition = 0;
  78.  
  79.  
  80. for (uint i = longestBit; i < longestBit + kLength; i++)
  81. {
  82. numberInBits.RemoveAt((int)i);
  83. numberInBits.Insert((int)i, firstValueOfBits[countingPosition]);
  84. countingPosition++;
  85. }
  86.  
  87. Console.WriteLine(converedNumber);
  88. string convertedResult = "";
  89.  
  90. for (int i = numberInBits.Count - 1; i >= 0; i--)
  91. {
  92. Console.Write(numberInBits[i]);
  93. convertedResult += numberInBits[i];
  94. }
  95.  
  96. Console.WriteLine();
  97. uint finalResult = Convert.ToUInt32(convertedResult, 2);
  98. Console.WriteLine(finalResult);
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement