Advertisement
BorisSimeonov

BitsExchangeAdvanced

Nov 10th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2.  
  3. class BitsExchangeAdvanced
  4. {
  5.     static void Main()
  6.     {
  7.         uint num = uint.Parse(Console.ReadLine());
  8.         int firstPosition = int.Parse(Console.ReadLine());
  9.         int secondPosition = int.Parse(Console.ReadLine());
  10.         int length = int.Parse(Console.ReadLine());
  11.  
  12.         //-----------------------
  13.         if(firstPosition + length > 32 || secondPosition + length > 32)
  14.         {
  15.             Console.WriteLine("Out of range");
  16.         }
  17.         else if ((firstPosition>secondPosition && secondPosition + length >= firstPosition) ||
  18.             (secondPosition>firstPosition && firstPosition + length >= secondPosition) ||
  19.             (firstPosition == secondPosition))
  20.         {
  21.             Console.WriteLine("Overlapping");
  22.         }
  23.         else
  24.         {
  25.  
  26.             for (int cnt = 0; cnt < length; cnt++)
  27.             {
  28.                 uint firstValue = (uint)(num >> (firstPosition + cnt)) & 1;
  29.                 uint secondValue = (uint)(num >> (secondPosition + cnt)) & 1;
  30.                 if (firstValue == 1 && secondValue == 0)
  31.                 {
  32.                     num -= (uint)(1 << (firstPosition + cnt));
  33.                     num += (uint)(1 << (secondPosition + cnt));
  34.                 }
  35.  
  36.                 if (secondValue == 1 && firstValue == 0)
  37.                 {
  38.                     num += (uint)(1 << (firstPosition + cnt));
  39.                     num -= (uint)(1 << (secondPosition + cnt));
  40.                 }
  41.             }
  42.             Console.WriteLine(num);
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement