Задача 11
Write an expression that extracts from a given integer i the value of a given bit number b. Example: i=5; b=2 -> value=1.using System; class GetBitNumberP { static void Main() { Console.Write("Enter number:"); int number = int.Parse(Console.ReadLine()); Console.Write("Enter bit position:"); int position = int.Parse(Console.ReadLine()); int mask = 1 << position; int maskAndNumber = number & mask; int pInNumber = maskAndNumber >> position; Console.WriteLine("In number {0} bit at position {1} is {2}", number, position, pInNumber); } }
Задача 12
We are given integer number n, value v (v=0 or 1) and a position p. Write a sequence of operators that modifies n to hold the value v at the position p from the binary representation of n.Example: n = 5 (00000101), p=3, v=1 -> 13 (00001101) n = 5 (00000101), p=2, v=0 -> 1 (00000001)
using System; class SetBitP { static void Main() { Console.Write("Enter number:"); int number = int.Parse(Console.ReadLine()); Console.Write("Enter bit position:"); int position = int.Parse(Console.ReadLine()); Console.Write("Enter new bit value:"); int newBitValue = int.Parse(Console.ReadLine()); int mask = 0; int newNumber = 0; if (newBitValue == 1) { mask = 1 << position; newNumber = number | mask; } else { mask = ~(1 << position); newNumber = number & mask; } Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0')); Console.WriteLine(Convert.ToString(mask, 2).PadLeft(32, '0')); Console.WriteLine(Convert.ToString(newNumber, 2).PadLeft(32, '0')); Console.WriteLine("The new number is {0}", newNumber); } }
Задача 13
Write a program that exchanges bits 3, 4 and 5 with bits 24, 25 and 26 of given 32-bit unsigned integer.using System; class ChangeBits345 { static void Main() { Console.Write("Enter number:"); int number = int.Parse(Console.ReadLine()); int mask = ~(7 << 3); Console.WriteLine(new String('-', 40)); Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0')); //Change bits 3, 4 & 5 to zeros number = number & mask; Console.WriteLine(Convert.ToString(mask, 2).PadLeft(32, '0')); Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0')); Console.WriteLine(new String('-', 40)); int newMask = number >> 21; Console.WriteLine(Convert.ToString(newMask, 2).PadLeft(32, '0')); int maskMask = 7 << 3; newMask = newMask & maskMask; Console.WriteLine(Convert.ToString(maskMask, 2).PadLeft(32, '0')); Console.WriteLine(Convert.ToString(newMask, 2).PadLeft(32, '0')); Console.WriteLine(new String('-', 40)); int newNumber = number | newMask; Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0')); Console.WriteLine(Convert.ToString(newMask, 2).PadLeft(32, '0')); Console.WriteLine(Convert.ToString(newNumber, 2).PadLeft(32, '0')); Console.WriteLine(new String('-', 40)); Console.WriteLine("The new number is {0}", newNumber); } }
Задача 14
Write a program that exchanges bits {p, p+1, …, p+k-1) with bits {q, q+1, …, q+k-1} of given 32-bit unsigned integer.using System; class ChangeCustomBits { static void Main() { Console.Write("Enter number:"); int number = int.Parse(Console.ReadLine()); Console.Write("Enter number of bits to change (k):"); int numberOfBitsToChange = int.Parse(Console.ReadLine()); Console.Write("Enter position of first bit to be changed (p):"); int startingBitToChange = int.Parse(Console.ReadLine()); Console.Write("Enter position of first bit to take (q) (q>p):"); int startingBitToTake = int.Parse(Console.ReadLine()); Console.WriteLine(new String('-', 40)); int maskOnes = 0; for (int i = 0; i < numberOfBitsToChange; i++) { maskOnes <<= 1; maskOnes |= 1; } Console.WriteLine(Convert.ToString(maskOnes, 2).PadLeft(32, '0')); int mask = ~(maskOnes << startingBitToChange); Console.WriteLine(new String('-', 40)); Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0')); //Change bits p, p+1 ... to zeros number = number & mask; Console.WriteLine(Convert.ToString(mask, 2).PadLeft(32, '0')); Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0')); Console.WriteLine(new String('-', 40)); //Making a new mask with the bits we want to set int newMask = number >> startingBitToTake; Console.WriteLine(Convert.ToString(newMask, 2).PadLeft(32, '0')); newMask = newMask & maskOnes; Console.WriteLine(Convert.ToString(maskOnes, 2).PadLeft(32, '0')); Console.WriteLine(Convert.ToString(newMask, 2).PadLeft(32, '0')); newMask <<= numberOfBitsToChange; Console.WriteLine(Convert.ToString(newMask, 2).PadLeft(32, '0')); Console.WriteLine(new String('-', 40)); int newNumber = number | newMask; Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0')); Console.WriteLine(Convert.ToString(newMask, 2).PadLeft(32, '0')); Console.WriteLine(Convert.ToString(newNumber, 2).PadLeft(32, '0')); Console.WriteLine(new String('-', 40)); Console.WriteLine("The new number is {0}", newNumber); } }
Няма коментари:
Публикуване на коментар