BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Logical Operator Overloading

The tenth article in the C# Object-Oriented Programming tutorial continues the discussion of operator overloading. In this article, the overloading of the logical operators is described including how to enable the short-circuit Boolean logical operators.

The Logical Operators

There are four logical operators that can be directly overloaded for a class. These are the NOT operator (!), the AND operator (&), the OR operator (|) and the exclusive OR or XOR operator (^). The short-circuit operators (&& and ||) cannot be overloaded directly. However, if the class meets certain conditions the short-circuit operators are enabled automatically.

This article uses the Vector class developed over previous articles. The Vector class represents a two-dimensional vector and already overloads arithmetic operators and the true and false operators. If you do not have the code, downlaod it using the link above.

In this article, the Vector class will be updated to include overloading of the four logical operators for Boolean operators.

Overloading the Binary Boolean Logical Operators

The Boolean binary logical operators are generally used in conditional processing operations and thus return a Boolean value, either true or false. To create an overloaded version of these operators, the syntax is the same as for other binary overloads, except that the return value is generally of the bool type.

public static bool operator logical-operator (
    op-type operand,
    op-type2 operand2
)

At least one of the operands must be of the same type as the containing class. This means that by providing the correct signature, you can permit logical operations between two classes of differing types.

Creating the Boolean AND Operator (&)

The three binary Boolean operators can be added to the Vector class using the syntax described above. When determining the results of each, a Vector will be deemed to equate to true if either of the co-ordinates is non-zero. If both the X and Y properties are zero, the Vector will be considered 'false'.

Add the following operator overload to the Vector class to implement the AND function:

public static bool operator &(Vector v1, Vector v2)
{
    bool v1flag = !((v1.X == 0) && (v1.Y == 0));
    bool v2flag = !((v2.X == 0) && (v2.Y == 0));

    return v1flag & v2flag;
}

The code evaluates each vector's co-ordinates to create a Boolean representation in the 'flag' variables. An AND operation is then performed on the two flags and the result returned. You can test the code by changing the Main method of the program as follows and executing the console application.

static void Main(string[] args)
{
    Vector v1 = new Vector(0, 0);
    Vector v2 = new Vector(10, 0);

    Console.WriteLine(v1 & v1);                 // Outputs "False"
    Console.WriteLine(v1 & v2);                 // Outputs "False"
    Console.WriteLine(v2 & v1);                 // Outputs "False"
    Console.WriteLine(v2 & v2);                 // Outputs "True"
}

Creating the Boolean OR (|) and XOR (^) Operators

Using similar code, we can add the Boolean OR and XOR operators to the Vector class. Add the following two operator overloads to the class to implement the two logical functions.

public static bool operator |(Vector v1, Vector v2)
{
    bool v1flag = !((v1.X == 0) && (v1.Y == 0));
    bool v2flag = !((v2.X == 0) && (v2.Y == 0));

    return v1flag | v2flag;
}

public static bool operator ^(Vector v1, Vector v2)
{
    bool v1flag = !((v1.X == 0) && (v1.Y == 0));
    bool v2flag = !((v2.X == 0) && (v2.Y == 0));

    return v1flag ^ v2flag;
}

Again, these operators can be tested using a modified Main method:

static void Main(string[] args)
{
    Vector v1 = new Vector(0, 0);
    Vector v2 = new Vector(10, 0);

    Console.WriteLine(v1 | v1);                 // Outputs "False"
    Console.WriteLine(v1 | v2);                 // Outputs "True"
    Console.WriteLine(v2 | v1);                 // Outputs "True"
    Console.WriteLine(v2 | v2);                 // Outputs "True"

    Console.WriteLine(v1 ^ v1);                 // Outputs "False"
    Console.WriteLine(v1 ^ v2);                 // Outputs "True"
    Console.WriteLine(v2 ^ v1);                 // Outputs "True"
    Console.WriteLine(v2 ^ v2);                 // Outputs "False"
}

Overloading the Unary Boolean Logical Operator

Only one unary Boolean logical operator exists. This is the NOT operator (!) that switches a Boolean value between true and false. When overloaded using the unary syntax below, the value returned should be the opposite of the Boolean representation of the object. As with other unary operators, the type of the operand provided must be the same as the class that the declaration appears within.

public static bool operator !(op-type operand)
26 November 2007