I have a simple program that calculates and outputs the results of all the bit operators. I'm planning to send it to my friend and to explain some stuff about them to him. Before I do that, I want to know if I can make any improvements to my program.
internal class Program
{
private static int a;
private static int b;
private static byte byteOperation;
private static short int16Operation;
private static int int32Operation;
private static long int64Operation;
private static void Main()
{
while (true)
{
bool numberA = int.TryParse(Console.ReadLine(), out a);
bool numberB = int.TryParse(Console.ReadLine(), out b);
if (!numberA || !numberB) Console.WriteLine("Input can be only numbers !");
else
{
Console.WriteLine();
byteOperation = (byte) (a & b);
int16Operation = (short) (a & b);
int32Operation = a & b;
int64Operation = a & b;
WriteLine("Binary AND", "&");
byteOperation = (byte) (a | b);
int16Operation = (short) (a | b);
int32Operation = a | b;
int64Operation = a | b;
WriteLine("Binary OR", "|");
byteOperation = (byte) (a ^ b);
int16Operation = (short) (a ^ b);
int32Operation = a ^ b;
int64Operation = a ^ b;
WriteLine("Binary XOR Operator", "^");
byteOperation = (byte) (a << b);
int16Operation = (short) (a << b);
int32Operation = a << b;
int64Operation = a << b;
WriteLine("Binary Left Shift", "<<");
byteOperation = (byte) (a >> b);
int16Operation = (short) (a >> b);
int32Operation = a >> b;
int64Operation = a >> b;
WriteLine("Binary Right Shift", ">>");
byteOperation = (byte) (a << b | a >> 8 - b);
int16Operation = (short) (a << b | a >> 16 - b);
int32Operation = a << b | a >> 32 - b;
int64Operation = a << b | a >> 64 - b;
WriteLine("Circular Shift", "<< | >>", "<<", "|", ">>");
}
}
}
private static void WriteLine(string text, string ch)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(text + " : " + ch);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Byte : {0} " + ch + " {1} = {2}", a, b, byteOperation);
Console.WriteLine("Int16 : {0} " + ch + " {1} = {2}", a, b, int16Operation);
Console.WriteLine("Int32 : {0} " + ch + " {1} = {2}", a, b, int32Operation);
Console.WriteLine("Int64 : {0} " + ch + " {1} = {2}", a, b, int64Operation);
Console.WriteLine();
}
private static void WriteLine(string text, string chFull, string ch1, string ch2, string ch3)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(text + " : " + chFull);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Byte : {0} " + ch1 + " {1} " + ch2 + " {0} " + ch3 + " 8 - {1}" + " = {2}", a, b, byteOperation);
Console.WriteLine("Int16 : {0} " + ch1 + " {1} " + ch2 + " {0} " + ch3 + " 16 - {1}" + " = {2}", a, b, int16Operation);
Console.WriteLine("Int32 : {0} " + ch1 + " {1} " + ch2 + " {0} " + ch3 + " 32 - {1}" + " = {2}", a, b, int32Operation);
Console.WriteLine("Int64 : {0} " + ch1 + " {1} " + ch2 + " {0} " + ch3 + " 64 - {1}" + " = {2}", a, b, int32Operation);
Console.WriteLine();
}
}