Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _15BitsExchange
- {
- class BitsExchange
- {
- static void Main()
- {
- Console.Write("Enter a number: ");
- uint n = uint.Parse(Console.ReadLine());
- Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));
- Console.WriteLine("This is our number.");
- Console.WriteLine();
- uint firstThreeBitsMoved = (n >> 3) ;
- Console.WriteLine(Convert.ToString(firstThreeBitsMoved, 2).PadLeft(32, '0'));
- Console.WriteLine("This is our number with the first 3 bits moved on the rightmost.");
- Console.WriteLine();
- uint seven = 7;
- Console.WriteLine(Convert.ToString(seven, 2).PadLeft(32, '0'));
- Console.WriteLine("This is the number seven (7).");
- Console.WriteLine();
- uint firstThreeBitsNew = firstThreeBitsMoved & 7;
- Console.WriteLine(Convert.ToString(firstThreeBitsNew, 2).PadLeft(32, '0'));
- Console.WriteLine("First 3 bits look like our number's but all others are zeros. Let's call it Phantom 1.");
- Console.WriteLine();
- int maskFirstBits = ~(7 << 24);
- Console.WriteLine(Convert.ToString(maskFirstBits, 2).PadLeft(32, '0'));
- Console.WriteLine("This is our mask for the first 3 bits. They are moved on the wanted position.");
- Console.WriteLine();
- uint secondThreeBitsMoved = (n >> 24);
- Console.WriteLine(Convert.ToString(secondThreeBitsMoved, 2).PadLeft(32, '0'));
- Console.WriteLine("Here we move the second 3 bits on the rightmost.");
- Console.WriteLine();
- uint secondThreeBitsNew = secondThreeBitsMoved & 7;
- Console.WriteLine(Convert.ToString(secondThreeBitsNew, 2).PadLeft(32, '0'));
- Console.WriteLine("Second 3 bits look like our number's but all others are zeros. Let's call it Phantom 2.");
- Console.WriteLine();
- int maskSecondBits = ~(7 << 3);
- Console.WriteLine(Convert.ToString(maskSecondBits, 2).PadLeft(32, '0'));
- Console.WriteLine("This is our mask for the second 3 bits. They are moved on the wanted position.");
- Console.WriteLine();
- n = n & (uint)maskFirstBits | (firstThreeBitsNew << 24);
- Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));
- Console.WriteLine("( Number & mask) | ( Phantom 1 with moved first 3 bits 24 positions to the left.");
- Console.WriteLine();
- n = n & (uint)maskSecondBits | (secondThreeBitsNew << 3);
- Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));
- Console.WriteLine("( Number & mask) | ( Phantom 2 with moved first 3 bits 3 positions to the left.");
- Console.WriteLine();
- for (int i = 0; i < Console.BufferWidth; i++)
- {
- Console.Write("*");
- }
- Console.WriteLine("The last one is actually our brand new number or that's: {0}.", n);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment