mustafov

Game of Bits

Sep 3rd, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. //Game of Bits
  2. //Vasko likes to play with odd and even numbers as well as bits. He has to make a game using bits but he really enjoys the course //of Web Fundamentals so he doesn't have time to make the game. Please help him!
  3. //You have a 32-bit integer and commands: "Odd", "Even" or "Game Over!" When you read the "Odd" command you have to obtain a new //number by extracting the values of all odd bit positions in the current number (positions are counted from right to left and the //first bit has a position of 1).  When you read the "Even" command you have to extract the bits at even positions. When you read //the command "Game Over!" you must print on the console the count of bits with value '1' in the final number.
  4.  
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace ConsoleApplication5
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             long num = long.Parse(Console.ReadLine());
  19.             string bin = Convert.ToString(num, 2);
  20.             string command = Console.ReadLine();
  21.  
  22.             long bit = 0;
  23.             long value = 0;
  24.             int index1 = 0;
  25.             int indexForBit = 0;
  26.             long rememberedNum = num;
  27.             while (command != "Game Over!")
  28.             {
  29.                 if (command == "Odd")
  30.                 {
  31.                     for (int i = bin.Length ; i >= 0 ; i--)
  32.                     {
  33.                        
  34.                         if (index1 % 2 == 0)
  35.                         {
  36.                             bit = num & (1 << index1);
  37.                             bit = bit >> index1;
  38.                             value = value | (bit << indexForBit);
  39.                             indexForBit++;
  40.                         }
  41.                         else
  42.                         {
  43.                         }
  44.                         index1++;
  45.                     }
  46.                     rememberedNum = value;
  47.                     num = value;
  48.                     bin = Convert.ToString(num, 2);
  49.                     index1 = 0;
  50.                     indexForBit = 0;
  51.                     value = 0;
  52.                 }
  53.                 else if (command == "Even")
  54.                 {
  55.                     for (int j = bin.Length; j >= 0; j--)
  56.                     {
  57.                         if (index1 % 2 != 0)
  58.                         {
  59.                             bit = num & (1 << index1);
  60.                             bit = bit >> index1;
  61.                             value = value | (bit << indexForBit);
  62.                             indexForBit++;
  63.                         }
  64.                         else
  65.                         {
  66.                         }
  67.                         index1++;
  68.                     }
  69.                     rememberedNum = value;
  70.                     num = value;
  71.                     bin = Convert.ToString(num, 2);
  72.                     index1 = 0;
  73.                     indexForBit = 0;
  74.                     value = 0;
  75.                 }
  76.                 command = Console.ReadLine();
  77.             }
  78.  
  79.             string binary = Convert.ToString(rememberedNum, 2);
  80.             int numberOfOnes = 0;
  81.  
  82.             for (int p = 0; p < binary.Length; p++)
  83.             {
  84.                 if (binary[p] == '1')
  85.                 {
  86.                     numberOfOnes++;
  87.                 }
  88.             }
  89.             Console.WriteLine("{0} -> {1}",rememberedNum,numberOfOnes);
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment