Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace bitbuilder
- {
- class Program
- {
- static void Main(string[] args)
- {
- int number = int.Parse(Console.ReadLine());
- string positionS = Console.ReadLine();
- string command = Console.ReadLine();
- while (command != "quit")
- {
- int result = 0;
- int bit = 0;
- int position = int.Parse(positionS);
- if (command == "flip")
- {
- number = number ^ (1 << position);
- }
- if (command == "insert")
- {
- for (int i = 0; i <= 31; i++)
- {
- if (i == position)
- {
- result = result | (1 << position);
- for (int j = i, p=i+1; j <= 31;++p, j++)
- {
- if(p==32)
- {
- p--;
- }
- bit = (number >> j) & 1;
- result = result | bit << p;
- //Console.WriteLine(Convert.ToString(result, 2).PadLeft(16, '0'));
- }
- break;
- }
- bit = (number >> i) & 1;
- result = result | bit << i;
- //Console.WriteLine(Convert.ToString(result, 2).PadLeft(16, '0'));
- }
- number = result;
- }
- if (command == "remove")
- {
- for (int i = 0; i <= 31; i++)
- {
- if (i == position)
- {
- for (int j = i,p=i+1; j <= 31; ++p,j++)
- {
- if (p == 32)
- {
- p--;
- }
- bit = (number >> p) & 1;
- result = result | bit << j;
- //Console.WriteLine(Convert.ToString(result, 2).PadLeft(16, '0'));
- }
- break;
- }
- bit = (number >> i) & 1;
- result = result | bit << i;
- //Console.WriteLine(Convert.ToString(result, 2).PadLeft(16, '0'));
- }
- number = result;
- }
- positionS = Console.ReadLine();
- if (positionS == "quit")
- {
- break;
- }
- command = Console.ReadLine();
- }
- //Console.WriteLine(Convert.ToString(number, 2).PadLeft(16, '0'));
- Console.WriteLine(number);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement