Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 ExchangeOfBits
- {
- static void Main()
- {
- uint number;
- int i,j;
- long valueBitPosition;
- int[] bitValue = new int[32];
- int[] newBitValue = new int[32];
- Console.WriteLine("Enter number > 0: ");
- while(!(uint.TryParse(Console.ReadLine(), out number)))
- {
- Console.WriteLine("Enter number > 0: ");
- }
- for (i = 0; i <= 31; i++)
- {
- valueBitPosition = ((1 << i) & number);
- if (valueBitPosition == 0)
- {
- bitValue[i] = 0;
- }
- else
- {
- bitValue[i] = 1;
- }
- }
- newBitValue[3] = bitValue[24];
- newBitValue[4] = bitValue[25];
- newBitValue[5] = bitValue[26];
- newBitValue[24] = bitValue[3];
- newBitValue[25] = bitValue[4];
- newBitValue[26] = bitValue[5];
- bitValue[3] = newBitValue[3];
- bitValue[4] = newBitValue[4];
- bitValue[5] = newBitValue[5];
- bitValue[24] = newBitValue[24];
- bitValue[25] = newBitValue[25];
- bitValue[26] = newBitValue[26];
- Array.Reverse(bitValue);
- Console.WriteLine(Convert.ToString(number,2).PadLeft(32,'0'));
- //Console.WriteLine(bitValue.Length);
- for (j = 0; j <= 31; j++)
- {
- Console.Write(bitValue[j]);
- }
- Console.WriteLine("\n");
- Main();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment