Advertisement
mustafov

Bit Lock

Sep 3rd, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. //Bit Lock
  2. //Your task is to write a program that tests a new kind of security lock which uses bitwise operations. The lock itself can be //represented as a table with 8 rows and 12 columns (see example below), where each cell contains a single bit (0 or 1).
  3. //You will be given 8 integers (representing the rows of the table) on a single line, separated by a single space.
  4. //Afterwards, you will be given a series of commands, between 1 and 30, which will be in one of the following three formats:
  5. //•   "check [col]", where [col] is a number. Upon receiving this command you'll need to check how many 1 bits there are in //column [col] and print their amount on the console.
  6. //•   "end" denotes the end of input. Upon receiving this command you need to print all rows of the table (as numbers) on a //single line, separated by a single space; print a space after the last number as well.
  7. //•   "[row] [direction] [rotations]", where [row] is a number; [direction] is a string, either "left" or "right"; and //[rotations] is also a number. Upon receiving this command, you need to roll the bits at the specified row. Rolling once to the //left means that all bits are moved once to the left, the bit at column 11 goes to column 0. Rolling once to the right means all //bits are moved once to the right, the bit at column 0 goes to column 11. The number of rotations shows how many times you have to //roll the bits on the specified row; it will be between 0 and 360 inclusive.
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14.  
  15. namespace ConsoleApplication4
  16. {
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             string input = Console.ReadLine();
  22.  
  23.             string[] nums = input.Split(' ');
  24.             string command;
  25.             do
  26.             {
  27.  
  28.                 command = Console.ReadLine();
  29.                 string[] comm = command.Split(' ');
  30.  
  31.                 string check = null;
  32.                 int numOfCheck = 0;
  33.                 int row = 0;
  34.                 string direction = null;
  35.                 int rotations = 0;
  36.  
  37.                 int bitValue = 0;
  38.  
  39.                 if (comm.Length == 2)
  40.                 {
  41.                     check = comm[0];
  42.                     numOfCheck = Convert.ToInt32(comm[1]);
  43.                 }
  44.                 else if (comm.Length == 3)
  45.                 {
  46.                     row = Convert.ToInt32(comm[0]);
  47.                     direction = comm[1];
  48.                     rotations = Convert.ToInt32(comm[2]);
  49.                 }
  50.                 int count = 0;
  51.                 if (check == "check")
  52.                 {
  53.                     for (int i = 0; i < 8; i++)
  54.                     {
  55.                         int num = Convert.ToInt32(nums[i]);
  56.                         bitValue = (num >> numOfCheck) & 1;
  57.                         if (bitValue == 1)
  58.                         {
  59.                             count++;
  60.                         }
  61.                     }
  62.                     Console.WriteLine(count);
  63.                 }
  64.                 if (direction == "right")
  65.                 {
  66.                     int num = Convert.ToInt32(nums[row]);
  67.                     int rotations1 = rotations % 12;
  68.                     int rotations2 = 12  - rotations1;
  69.                     int mask1 = (num >> rotations1);
  70.                     int mask2 = (num << rotations2);
  71.                     num = mask1 | mask2;
  72.                     int number = num & 4095;
  73.                     nums[row] = Convert.ToString(number);
  74.                 }
  75.  
  76.                 else if (direction == "left")
  77.                 {
  78.                     int num = Convert.ToInt32(nums[row]);
  79.                     int rotations1 = rotations % 12;
  80.                     int rotations2 = 12 - rotations1;
  81.                     int mask1 = (num << rotations1);
  82.                     int mask2 = (num >> rotations2);
  83.                     num = mask1 | mask2;
  84.                     int number = num & 4095;
  85.                     nums[row] = Convert.ToString(number);
  86.                 }
  87.             } while (command != "end");
  88.             for (int j = 0; j < 8; j++)
  89.             {
  90.                 Console.Write(nums[j] + " ");
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement