Advertisement
darighteous1

05. GameOfBits

Mar 31st, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using System;
  2.  
  3. class GameOfBits
  4. {
  5.     static void Main()
  6.     {
  7.         uint number = uint.Parse(Console.ReadLine());
  8.         string command = Console.ReadLine();
  9.  
  10.         while (command != "Game Over!")
  11.         {
  12.             int count = 0;
  13.  
  14.             switch (command)
  15.             {
  16.                 case "Odd":
  17.                     count = 0;
  18.                     break;
  19.                 case "Even":
  20.                     count = 1;
  21.                     break;
  22.             }
  23.             uint newNumber = 0;
  24.             int index = 0;
  25.             for (; count <= 31; count += 2, index++)
  26.             {
  27.  
  28.                 uint currentBit = ((number >> count) & 1) << index;
  29.                 newNumber = newNumber | currentBit;
  30.             }
  31.             number = newNumber;
  32.             command = Console.ReadLine();
  33.         }
  34.  
  35.         int countOneBits = 0;
  36.         for (int i = 0; i < 32; i++)
  37.         {
  38.             uint currentBit = (number >> i) & 1;
  39.             if (currentBit == 1)
  40.             {
  41.                 countOneBits++;
  42.             }
  43.         }
  44.  
  45.         Console.WriteLine("{0} -> {1}", number, countOneBits);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement