Beispiel #1
0
        /* Prints the set operators' (AND, OR, XOR) results for the current and given object.
         * Uses Thread to print row by row in a timed manner. */
        public void PrintSetOperations(Bitwise b2)
        {
            if (b2 == null)
            {
                return;
            }

            // Equalize lengths for presentation
            int len1 = this.NumberBinary.Length;
            int len2 = b2.NumberBinary.Length;

            if (len1 > len2)
            {
                b2.NumberBinary = InsertLeadingZeros(b2.NumberBinary, len1 - len2);
            }
            else if (len2 > len1)
            {
                this.NumberBinary = InsertLeadingZeros(this.NumberBinary, len2 - len1);
            }


            // AND
            Thread.Sleep(500);
            Console.WriteLine();
            Console.WriteLine(this);
            Console.WriteLine("AND");
            Thread.Sleep(500);
            Console.WriteLine(b2);
            Thread.Sleep(500);
            Console.WriteLine("=");
            Thread.Sleep(1000);
            Console.WriteLine(BitwiseAnd(b2.NumberBinary));
            Console.WriteLine();

            // OR
            Thread.Sleep(500);
            Console.WriteLine();
            Console.WriteLine(this);
            Console.WriteLine("OR");
            Thread.Sleep(500);
            Console.WriteLine(b2);
            Thread.Sleep(500);
            Console.WriteLine("=");
            Thread.Sleep(1000);
            Console.WriteLine(BitwiseOr(b2.NumberBinary));
            Console.WriteLine();

            // XOR
            Thread.Sleep(500);
            Console.WriteLine();
            Console.WriteLine(this);
            Console.WriteLine("XOR");
            Thread.Sleep(500);
            Console.WriteLine(b2);
            Thread.Sleep(500);
            Console.WriteLine("=");
            Thread.Sleep(1000);
            Console.WriteLine(BitwiseXor(b2.NumberBinary));
            Console.WriteLine();



            Thread.Sleep(500);
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            // All the commands that trigger quit
            string[]     Quit           = { "Q", "q", "quit", "QUIT", "Quit" };
            const string WelcomeMessage = "\n\n*** Welcome to Bitwise Visualizer! ***\n";

            Console.WriteLine(WelcomeMessage);

            bool cont = true;

            while (cont)
            {
                Console.Write("Give number or (Q)uit: ");
                string selection = Console.ReadLine();

                if (Array.IndexOf(Quit, selection) > -1)
                {
                    cont = false;
                }
                else
                {
                    if (uint.TryParse(selection, out uint newNum))
                    {
                        Bitwise b = new Bitwise(newNum);


                        // Print NOT, << 1, and >> 1
                        b.PrintNonSetOperations();

                        // Get a second binary string for the other operations
                        Bitwise b2 = null;
                        bool    validBinaryString = false;

                        while (!validBinaryString)
                        {
                            Console.WriteLine();
                            Console.Write("Give a binary string: ");
                            string bString = Console.ReadLine();


                            if (Array.IndexOf(Quit, bString) > -1)
                            {
                                //Environment.Exit(0);
                                cont = false;
                                validBinaryString = true;
                            }
                            else
                            {
                                long b2Int = Bitwise.ConvertToDecimal(bString);
                                if (b2Int > -1 && b2Int <= uint.MaxValue)
                                {
                                    b2 = new Bitwise((uint)b2Int);
                                    validBinaryString = true;
                                }
                                else
                                {
                                    Console.WriteLine();
                                    Console.WriteLine("Not a valid binary string.");
                                    Console.WriteLine("Binary strings can only have 1s and 0s.");
                                }
                            }
                        }

                        // Unless the user wants to quit, print AND, OR, and XOR
                        if (cont)
                        {
                            b.PrintSetOperations(b2);

                            Console.WriteLine();
                            Console.Write("Press Enter to Continue");
                            Console.ReadLine();
                            Console.Clear();
                            Console.WriteLine(WelcomeMessage);
                        }
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("Invalid number");
                        Console.WriteLine();
                    }
                }
            }
        }