TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# XOR Operator

This C# tutorial demonstrates the use of the bitwise XOR operator.

XOR (exclusive or) modifies a bit pattern. If both operands have a bit set, the result does not have that bit set.

If neither has a bit set, the result also does not have that bit set. But if one has the bit set, the result is set.

Binary Representation

Example. To begin, let's look at an example program. The XOR operator is a binary operator, meaning is requires two operands. As a reminder, an operand is a numeric argument to the operator (the numbers on each side of the "^" caret symbol).

In this program, we use a method that shows us the bits that are set in each number. We see the bits set in the first operand, the second operand, and the result returned by the XOR operation.

C# program that uses XOR operator

using System;

class Program
{
    static void Main()
    {
	// Demonstrate XOR for two integers.
	int a = 5550 ^ 800;
	Console.WriteLine(GetIntBinaryString(5550));
	Console.WriteLine(GetIntBinaryString(800));
	Console.WriteLine(GetIntBinaryString(a));
	Console.WriteLine();

	// Repeat.
	int b = 100 ^ 33;
	Console.WriteLine(GetIntBinaryString(100));
	Console.WriteLine(GetIntBinaryString(33));
	Console.WriteLine(GetIntBinaryString(b));
    }

    /// <summary>
    /// Returns binary representation string.
    /// </summary>
    static string GetIntBinaryString(int n)
    {
	char[] b = new char[32];
	int pos = 31;
	int i = 0;

	while (i < 32)
	{
	    if ((n & (1 << i)) != 0)
	    {
		b[pos] = '1';
	    }
	    else
	    {
		b[pos] = '0';
	    }
	    pos--;
	    i++;
	}
	return new string(b);
    }
}

Output

00000000000000000001010110101110
00000000000000000000001100100000
00000000000000000001011010001110

00000000000000000000000001100100
00000000000000000000000000100001
00000000000000000000000001000101

Please look at the ones and zeros in the output section. The first two lines of each set are the bits of the operands. The third line of each set is the result from the "^" operator applied to those operands.

Tip: In places where only one bit is set in both operands, the result value has that same bit set. Otherwise no bits were set.

Discussion. Bitwise operators are exceedingly useful in some cases, but sadly not in many programs today. Typically, if you don't have limited memory on your system, you can replace bitwise operations with arrays.

Arrays

For some programs, such as those that use digital trees, you can use bitwise operators to test the bit patterns at each node. This yields an efficient search tree. Bitwise operators can also be used for hash code computations.

DAWGGetHashCode

Summary. The XOR binary operator receives two operands, and returns a number that contains a bit set for each exclusive bit set in an operand. In many programs, this operator is more trouble than it is worth, and will likely cause confusion.

Warning: Please proceed with the XOR operator with caution. It is sometimes helpful but often not.

Optimizations


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf