Skip to content

Instantly share code, notes, and snippets.

@Kiso-blg
Created March 13, 2016 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kiso-blg/79dd263e4796203dfcb6 to your computer and use it in GitHub Desktop.
Save Kiso-blg/79dd263e4796203dfcb6 to your computer and use it in GitHub Desktop.
C# Basics December 2014 Lab - 03.Byte_Party
using System;
namespace _03.Byte_Party
{
class ByteParty
{
static void Main(string[] args)
{
int length = int.Parse(Console.ReadLine());
int[] table = GetSequence(length);
PerformCommands(table);
for (int i = 0; i < table.Length; i++)
{
Console.WriteLine(table[i]);
}
}
static int[] GetSequence(int length)
{
int[] seqence = new int[length];
for (int i = 0; i < length; i++)
{
seqence[i] = int.Parse(Console.ReadLine());
}
return seqence;
}
static int[] PerformCommands(int[] sequence)
{
string command = Console.ReadLine();
while (command != "party over")
{
string[] orderAndPosition = command.Split(' ');
int order = int.Parse(orderAndPosition[0]);
int position = int.Parse(orderAndPosition[1]);
if (order == -1)
{
sequence = FlipBitAtPosition(sequence, position);
}
else if (order == 0)
{
sequence = UnsetBitAtPosition(sequence, position);
}
else if (order == 1)
{
sequence = SetBitAtPosition(sequence, position);
}
command = Console.ReadLine();
}
return sequence;
}
private static int[] SetBitAtPosition(int[] sequence, int position)
{
for (int i = 0; i < sequence.Length; i++)
{
sequence[i] |= (1 << position);
}
return sequence;
}
private static int[] UnsetBitAtPosition(int[] sequence, int position)
{
for (int i = 0; i < sequence.Length; i++)
{
if (((sequence[i] >> position) & 1) == 1)
{
sequence[i] ^= (1 << position);
}
}
return sequence;
}
static int[] FlipBitAtPosition(int[] sequence, int position)
{
for (int i = 0; i < sequence.Length; i++)
{
if (((sequence[i] >> position) & 1) == 1)
{
sequence[i] ^= (1 << position);
}
else
{
sequence[i] |= (1 << position);
}
}
return sequence;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment