← Back to list
Lab 1

Lab 1 — Boolean Logic

Boolean operations: AND, OR, XOR. MVVM pattern introduction.

Zhilinsky Egor Author

Purpose

Learn Boolean logic operations in C# and apply the Model–View–ViewModel (MVVM) architectural pattern.

Step 1 — Console program (integer version)

using System;
class Boole
{
    static public void Main(string[] args)
    {
        int x = Convert.ToInt32(args[0]);  // boole.exe 0 1
        int y = Convert.ToInt32(args[1]);
        int and = x & y;   // 0 & 1 = 0
        int or  = x | y;   // 0 | 1 = 1
        int xor = x ^ y;   // 0 ^ 1 = 1
        Console.WriteLine(x + " & " + y + "=" + and);
        Console.WriteLine(x + " | " + y + "=" + or);
        Console.WriteLine(x + " ^ " + y + "=" + xor);
    }
}

Step 2 — Boolean version

bool x = Convert.ToBoolean(args[0]);  // boole.exe False True
bool y = Convert.ToBoolean(args[1]);
bool and = x & y;   // False & True = False
bool or  = x | y;   // False | True = True
bool xor = x ^ y;   // False ^ True = True

Step 3 — Refactor to MVVM pattern

Separate Input/Output (View) from business logic (Model) into distinct methods:

class Boole
{
    static int x, y, and, or, xor;

    static public void Main(string[] args)
    {
        x = Convert.ToInt32(args[0]);
        y = Convert.ToInt32(args[1]);
        ViewModel();
    }

    static void ViewModel() { Model(); View(); }

    static void Model()
    {
        and = x & y;
        or  = x | y;
        xor = x ^ y;
    }

    static void View()
    {
        Console.WriteLine(x + " & " + y + "=" + and);
        Console.WriteLine(x + " | " + y + "=" + or);
        Console.WriteLine(x + " ^ " + y + "=" + xor);
    }
}

Step 4 — Migrate to Web (ASP.NET)

The MVVM separation makes it easy to migrate to a Web Form. Replace Console.WriteLine with Label.Text and read inputs from TextBox controls instead of args[].

Live Demo

Pick two boolean values and compute AND, OR, XOR on the server.

Code-behind (C#)

protected void btnCompute_Click(object sender, EventArgs e)
{
    bool x = Convert.ToBoolean(ddlX.SelectedValue);
    bool y = Convert.ToBoolean(ddlY.SelectedValue);

    bool and = x & y;
    bool or  = x | y;
    bool xor = x ^ y;

    litAnd.Text = x + " & " + y + " = " + and;
    litOr.Text  = x + " | " + y + " = " + or;
    litXor.Text = x + " ^ " + y + " = " + xor;

    pnlResult.Visible = true;
}

Conclusion

Boolean logic operations (&, |, ^) work the same for both int and bool types. The MVVM pattern cleanly separates UI from logic, enabling easy migration between console, desktop, and web applications.

Web hosting by Somee.com