Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Bit Lock
- //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).
- //You will be given 8 integers (representing the rows of the table) on a single line, separated by a single space.
- //Afterwards, you will be given a series of commands, between 1 and 30, which will be in one of the following three formats:
- //• "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.
- //• "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.
- //• "[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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication4
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- string[] nums = input.Split(' ');
- string command;
- do
- {
- command = Console.ReadLine();
- string[] comm = command.Split(' ');
- string check = null;
- int numOfCheck = 0;
- int row = 0;
- string direction = null;
- int rotations = 0;
- int bitValue = 0;
- if (comm.Length == 2)
- {
- check = comm[0];
- numOfCheck = Convert.ToInt32(comm[1]);
- }
- else if (comm.Length == 3)
- {
- row = Convert.ToInt32(comm[0]);
- direction = comm[1];
- rotations = Convert.ToInt32(comm[2]);
- }
- int count = 0;
- if (check == "check")
- {
- for (int i = 0; i < 8; i++)
- {
- int num = Convert.ToInt32(nums[i]);
- bitValue = (num >> numOfCheck) & 1;
- if (bitValue == 1)
- {
- count++;
- }
- }
- Console.WriteLine(count);
- }
- if (direction == "right")
- {
- int num = Convert.ToInt32(nums[row]);
- int rotations1 = rotations % 12;
- int rotations2 = 12 - rotations1;
- int mask1 = (num >> rotations1);
- int mask2 = (num << rotations2);
- num = mask1 | mask2;
- int number = num & 4095;
- nums[row] = Convert.ToString(number);
- }
- else if (direction == "left")
- {
- int num = Convert.ToInt32(nums[row]);
- int rotations1 = rotations % 12;
- int rotations2 = 12 - rotations1;
- int mask1 = (num << rotations1);
- int mask2 = (num >> rotations2);
- num = mask1 | mask2;
- int number = num & 4095;
- nums[row] = Convert.ToString(number);
- }
- } while (command != "end");
- for (int j = 0; j < 8; j++)
- {
- Console.Write(nums[j] + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement