[Домашно] Operators Expressions and statements - Problem 15

Здравейте,

Исках да представя моето решение на 15та задача от третата тема за коментари и копиране при желание.

Проверена е с примерните решения.

 

class BitsExchange

{
static void Main()
{
uint n;
Console.Write("This will change bit 3,4,5 with bits 24,25,26 of the following integer: ");
string sn = Console.ReadLine();
if (UInt32.TryParse(sn, out n))
{
for (int i = 0; i < 3; i++)
{
int smallP = 3 + i; //get the position of lower sequence
int bigP = 24 + i; //get the position of higher sequence
uint smallV = (n >> (smallP)) & 1; //get the lower bit
uint bigV = (n >> (bigP)) & 1; //get the higher bit
uint sequence = smallV + 2*bigV; //add both bits to sequence to control the switch
uint mask;
switch (sequence)
{
case 1: //smallV = 1 & bigV = 0
mask = 1u << bigP;
n = n | mask;
mask = ~(1u << smallP);
n = n & mask;
break;
case 2: //smallV = 0 & bigV = 1
mask = 1u << smallP;
n = n | mask;
mask = ~(1u << bigP);
n = n & mask;
break;
}
}
Console.WriteLine("The new integer is: " + n);
}
else
{
Console.WriteLine("This is not integer!");
}
}
}