c sharp

C# XOR Operator

The XOR (exclusive OR) operator is a binary operator that works with two operands and only yields true if and when precisely one of them evaluates to true. Besides, false is returned. Your talents as a C# programmer may be substantially increased by comprehending and using the XOR operator, whether it’s for logical decision-making or data manipulation. In this article, we will deliberate its uses in C# with the help of some code snippets.

Example 1:

Let’s go through a C# program that demonstrates the use of the XOR operator (^) to determine the healthiness of food based on whether it is vegetarian or contains meat. The first line “using System;” is a namespace declaration that allows the program to access the classes and functions that are defined in the “System” namespace which contains the fundamental types and core functionality of C#. The code defines a public class named “Dummy”.

The public keyword specifies that the class can be gotten into from other parts of the program. Inside the “Dummy” class, there is a static method named “Main”. The “Main” method takes an array of strings (string[] args) as a parameter which allows passing the command-line arguments to the program.

Within the “Main” method, three Boolean variables are declared and initialized: “isVeg” is set to true which indicates whether the food is vegetarian, “isMeat” is set to false which indicates whether the food contains meat, “IsFit” is declared and assigned with the result of the XOR operation (isVeg ^ isMeat) which determines the healthiness of the food based on its vegetarian status and meat content.

The Console.WriteLine line outputs the result of the healthiness check using the string interpolation. The value of “isFit” is displayed within the string which indicates whether the food is considered healthy or not.

using System;

public class Dummy {

  public static void Main(string[] args)

  {

    bool isVeg = true;

    bool isMeat = false;

    bool isFit = isVeg ^ isMeat;

    Console.WriteLine($"Is the food healthy? : {isFit}");

  }

}

To summarize, this code showcases the concept of Boolean logic in C# and how the XOR operator can be used to determine the logical XOR of two Boolean variables and evaluate the healthiness of food based on whether it is vegetarian or covers meat.

 

Example 2:

Let’s demonstrate the usage of the XOR operator in C# via another code example. We will perform a bitwise XOR operation on two integer variables (n1 and n2). The code begins with the “using System;” statement which imports the “System” namespace to allow the usage of classes that are defined in the “System” namespace such as the “Console” class.

The code defines a class named “Check” using the “public class check” syntax. This class contains a single method which is “Mai”. The main() function takes the “args” string array as a parameter which can be utilized to pass the command-line values to the code program.

Inside the Main() method, two integer variables, “n1” and “n2”, are declared and initialized with the values of 17 and 8, respectively. The “n1” value has a binary representation of 10001 which means that it is equal to decimal 17, and the “n2” value has a binary representation of 1000 which means that it is equal to decimal 8.

The “int Res = n1 ^ n2;” line calculates the result of the XOR operation between “n1” and “n2” using the XOR operator (^). The outcome is kept in the “Res” mutable. The XOR operation is applied to the binary representations of “n1” and “n2”. It compares each corresponding bit of the binary representations and sets the resulting bit to 1 if the bits are different and 0 if the bits are the same.

Performing the XOR operation, we get the binary result of 11001 which is equal to decimal 25. The Console.WriteLine($”Result: {Res}”); line prints the result of the XOR operation to the console. The Console.WriteLine method is used to display the “Result:” string followed by the value of the “Res” variable. The {$”Result: {Res}”} syntax is called string interpolation which allows us to embed the value of the “Res” variable directly into the string.

using System;

public class Check {

  public static void Main(string[] args)

  {

     int n1 = 17; // 10001

     int n2 = 8; // 1000

     int Res = n1 ^ n2;

     Console.WriteLine($"Result: {Res}");

  }

}

The output console screen that is shown in the following image displays the “25” result that is yielded by the use of the XOR operation between 17 and 8:

Example 3:

Let’s move towards the last example of this article that determines the usage of the XOR operator in C#. The following attached code snippet starts with the “using System;” statement which is importing the “System” namespace.

Next, a public class named “Test” is declared. This class is casted-off as the starting point here and also covers the main() method. Inside the “Main()” method, we perform the XOR operation on character values. Two “char” variables, “L1” and “L2”, are declared and assigned with the “M” and “R” values, respectively.

These variables represent the characters. Another “char” variable named “Res” is declared and assigned with the result of the XOR operation between “L1” and “L2”. The XOR operation is executed via the XOR operator “^”.

Since the XOR operator works on integer values, the (char) cast is used to convert the resulting integer value back to a character. The Console.WriteLine statement is cast-off to print the result. It uses the string interpolation (`$`) to include the value of “Res” in the output.

When the program is executed, the main() method is called and the following output is displayed in the console. In this case, the XOR operation between the “M” and “R” characters results in the “?” or “.” character. The resulting character is printed as part of the output.

using System;

public class Test {

  public static void Main(string[] args)

  {

    char L1 = 'M';

    char L2 = 'R';

    char Res = (char)(L1 ^ L2);

    Console.WriteLine($"Result: {Res}");

  }

}

That’s a detailed explanation of the previously provided code. It showcases that the XOR operator is used to perform a Bitwise XOR operation on character values and print the result as displayed in the following attached image:

Conclusion

The C# XOR operator provides a flexible tool to carry out the Bitwise and logical operations. It allows you to work with binary bits and express the unique conditions in Boolean logic. Using the code examples, we discussed how the XOR operator can be applied to Boolean values, integer values, and character values to get the desired output. Using the XOR operator properly, you can enhance your skills as a C# programmer.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.