Advertisement
ivanov_ivan

GameOfBits

Oct 29th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 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 PrimitiveDataTypesAndVariables
  8.     {
  9.     class GameOfBits
  10.         {
  11.         static void Main()
  12.             {
  13.             uint input = uint.Parse(Console.ReadLine());
  14.             string command = Console.ReadLine();
  15.             while (command != "Game Over!")
  16.                 {
  17.                 if (command == "Odd")
  18.                     {
  19.                     uint oddBitsValues = 0;
  20.                     uint oddBits = 0;
  21.                     for (int i = 31; i >= 0; i -= 2)
  22.                         {
  23.                         oddBitsValues = 1 & (input >> (i - 1));
  24.                         oddBits |= oddBitsValues << (i / 2);
  25.                         }
  26.                     input = oddBits;
  27.                     command = Console.ReadLine();
  28.                     }
  29.                 if (command == "Even")
  30.                     {
  31.                     uint evenBitsValues = 0;
  32.                     uint evenBits = 0;
  33.                     for (int i = 31; i >= 0; i -= 2)
  34.                         {
  35.                         evenBitsValues = 1 & (input >> (i));
  36.                         evenBits |= evenBitsValues << (i / 2);
  37.                         }
  38.                     input = evenBits;
  39.                     command = Console.ReadLine();
  40.                     }
  41.                 }
  42.             int count = 0;
  43.             char[] bitValues = Convert.ToString(input,2).ToCharArray();
  44.             for (int i = 0; i < bitValues.Length; i++)
  45.                 {
  46.                 int bit = (int)char.GetNumericValue(bitValues[i]);
  47.                 count += bit;
  48.                 }
  49.             Console.WriteLine("{0} -> {1}", input,count);
  50.             }
  51.         }
  52.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement