Advertisement
dimipan80

3.16 Bits Exchange (Advanced)

Jun 5th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. // Write a program that exchanges bits {p, p+1, …, p+k-1} with bits {q, q+1, …, q+k-1} of a given 32-bit unsigned integer. The first and the second sequence of bits may not overlap.
  2.  
  3. namespace _16.BitExchange_Advanced
  4. {
  5.     using System;
  6.  
  7.     public class BitExchange_Advanced
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             checked
  12.             {
  13.                 Console.Write("Enter a whole 32-bits non-negative number: ");
  14.                 uint num = uint.Parse(Console.ReadLine());
  15.                 Console.Write("Enter index for start position of First Bit-sequence: ");
  16.                 int indexP = int.Parse(Console.ReadLine());
  17.                 Console.Write("Enter index for start position of Second Bit-sequence: ");
  18.                 int indexQ = int.Parse(Console.ReadLine());
  19.                 Console.Write("Enter a whole positive number for lenght of Bit-sequences: ");
  20.                 uint lenghtK = uint.Parse(Console.ReadLine());
  21.  
  22.                 bool inputDataIsOutOfRange = (indexP < 0 || indexP + lenghtK - 1 > 31) || (indexQ < 0 || indexQ + lenghtK - 1 > 31) || lenghtK > 16;
  23.                 if (!inputDataIsOutOfRange)
  24.                 {
  25.                     bool bitSequncesOverlapping = (indexQ > indexP && indexP + lenghtK - 1 >= indexQ) || (indexP > indexQ && indexQ + lenghtK - 1 >= indexP) || indexP == indexQ;
  26.                     if (!bitSequncesOverlapping)
  27.                     {
  28.                         // First checking bit values on Bit Sequences. For this we needs from bit mask for all bits in the Sequence. That is depends from lenghtK:
  29.                         long bitMaskLenght = (long)((1 << (int)lenghtK) - 1);
  30.  
  31.                         // Getting bit values from First Sequence:
  32.                         long bitValuesOnFirstSequence = ((long)num >> indexP) & bitMaskLenght;
  33.  
  34.                         // Getting bit values from Second Sequence:
  35.                         long bitValuesOnSecondSequence = ((long)num >> indexQ) & bitMaskLenght;
  36.  
  37.                         // If founded bit values is equals, the exchange does not necessary and given number is output result.
  38.                         uint resultNum = num;
  39.                         if (bitValuesOnFirstSequence != bitValuesOnSecondSequence)
  40.                         {
  41.                             // Now must nullifying bits on given positions:
  42.                             long bitMaskOnFirstSeq = bitValuesOnFirstSequence << indexP;
  43.                             resultNum = (uint)(num ^ bitMaskOnFirstSeq);
  44.                             long bitMaskOnSecondSeq = bitValuesOnSecondSequence << indexQ;
  45.                             resultNum ^= (uint)bitMaskOnSecondSeq;
  46.  
  47.                             // Exchages the bits on positions of First Sequence:
  48.                             long bitMaskExchangeFirstSeq = bitValuesOnSecondSequence << indexP;
  49.                             resultNum |= (uint)bitMaskExchangeFirstSeq;
  50.  
  51.                             // Exchages the bits on positions of Second Sequence:
  52.                             long bitMaskExchangeSecondSeq = bitValuesOnFirstSequence << indexQ;
  53.                             resultNum |= (uint)bitMaskExchangeSecondSeq;                        
  54.                         }
  55.  
  56.                         Console.WriteLine("The Number After Bits Exchange is: {0} !", resultNum);
  57.                     }
  58.                     else
  59.                     {
  60.                         Console.WriteLine("Error! - BitSequences on these Positions Overlapping!!!");
  61.                     }
  62.                 }
  63.                 else
  64.                 {
  65.                     Console.WriteLine("Error!!! - Input Data is Out of Range!!!");
  66.                 }
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement