remote87

Bits Exchange

Aug 31st, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _15BitsExchange
  8. {
  9.     class BitsExchange
  10.     {
  11.         static void Main()
  12.         {
  13.             Console.Write("Enter a number: ");
  14.             uint n = uint.Parse(Console.ReadLine());
  15.             Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));
  16.             Console.WriteLine("This is our number.");
  17.             Console.WriteLine();
  18.             uint firstThreeBitsMoved = (n >> 3) ;
  19.             Console.WriteLine(Convert.ToString(firstThreeBitsMoved, 2).PadLeft(32, '0'));
  20.             Console.WriteLine("This is our number with the first 3 bits moved on the rightmost.");
  21.             Console.WriteLine();
  22.             uint seven = 7;
  23.             Console.WriteLine(Convert.ToString(seven, 2).PadLeft(32, '0'));
  24.             Console.WriteLine("This is the number seven (7).");
  25.             Console.WriteLine();
  26.             uint firstThreeBitsNew = firstThreeBitsMoved & 7;
  27.             Console.WriteLine(Convert.ToString(firstThreeBitsNew, 2).PadLeft(32, '0'));
  28.             Console.WriteLine("First 3 bits look like our number's but all others are zeros. Let's call it Phantom 1.");
  29.             Console.WriteLine();
  30.             int maskFirstBits = ~(7 << 24);
  31.             Console.WriteLine(Convert.ToString(maskFirstBits, 2).PadLeft(32, '0'));
  32.             Console.WriteLine("This is our mask for the first 3 bits. They are moved on the wanted position.");
  33.             Console.WriteLine();
  34.             uint secondThreeBitsMoved = (n >> 24);
  35.             Console.WriteLine(Convert.ToString(secondThreeBitsMoved, 2).PadLeft(32, '0'));
  36.             Console.WriteLine("Here we move the second 3 bits on the rightmost.");
  37.             Console.WriteLine();
  38.             uint secondThreeBitsNew = secondThreeBitsMoved & 7;
  39.             Console.WriteLine(Convert.ToString(secondThreeBitsNew, 2).PadLeft(32, '0'));
  40.             Console.WriteLine("Second 3 bits look like our number's but all others are zeros. Let's call it Phantom 2.");
  41.             Console.WriteLine();
  42.             int maskSecondBits = ~(7 << 3);
  43.             Console.WriteLine(Convert.ToString(maskSecondBits, 2).PadLeft(32, '0'));
  44.             Console.WriteLine("This is our mask for the second 3 bits. They are moved on the wanted position.");
  45.             Console.WriteLine();
  46.             n = n & (uint)maskFirstBits | (firstThreeBitsNew << 24);
  47.             Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));
  48.             Console.WriteLine("( Number & mask) | ( Phantom 1 with moved first 3 bits 24 positions to the left.");
  49.             Console.WriteLine();
  50.             n = n & (uint)maskSecondBits | (secondThreeBitsNew << 3);
  51.             Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));
  52.             Console.WriteLine("( Number & mask) | ( Phantom 2 with moved first 3 bits 3 positions to the left.");
  53.             Console.WriteLine();
  54.             for (int i = 0; i < Console.BufferWidth; i++)
  55.             {
  56.                 Console.Write("*");
  57.             }
  58.             Console.WriteLine("The last one is actually our brand new number or that's: {0}.", n);
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment