Earn More by Sharing What You Love
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Tim Corey
5m 54s
Binary operations are a crucial part of programming, particularly when dealing with low-level optimizations and bitwise manipulations. One such operation is the exclusive OR (XOR) operator. In this article, we will explore C#'s Binary XOR operator by following along with Tim Corey's video, "The Binary XOR Operator in 10 Minutes or Less."
Tim's video is an excellent breakdown of XOR, its syntax, and its practical applications. Below, we will analyze his explanations and examples while referencing specific timestamps for easy reference.
Tim begins the video by mentioning that this is part of his Understanding Binary in C# series, specifically lesson number nine. He sets the stage by explaining that in this session, he will teach how the exclusive OR (XOR) operator works and how it can be useful in various scenarios.
Tim introduces two values, 1 and 2, and prints them to the console for reference. He removes the 32-bit representation to keep the focus on the standard 8-bit binary representation.
At 0:31, he explains that XOR is represented by the caret symbol (^) in C#. He reminds viewers that:
Tim further explains the rule for each bit comparison:
This means that XOR acts as a difference detector between two values.
Tim moves to a practical example by defining a uint variable that holds the result of XOR-ing two values:
// Define two sample values
uint val1 = 0b0001; // 1 in decimal
uint val2 = 0b0010; // 2 in decimal
// Perform XOR operation between val1 and val2
uint result = val1 ^ val2;
// Print the resultant binary value of XOR operation
Console.WriteLine(Convert.ToString(result, toBase: 2).PadLeft(8, '0'));' Define two sample values
Dim val1 As UInteger = &B0001 ' 1 in decimal
Dim val2 As UInteger = &B0010 ' 2 in decimal
' Perform XOR operation between val1 and val2
Dim result As UInteger = val1 Xor val2
' Print the resultant binary value of XOR operation
Console.WriteLine(Convert.ToString(result, toBase:=2).PadLeft(8, "0"c))He then prints the result to the console and notes the output:
0000 0011
This output highlights that XOR only preserves bits where one of the original values had a 1, but not both.
Tim now demonstrates one practical use of XOR: checking if two values are identical.
At 3:00, he modifies the values so that val1 and val2 are exactly the same:
// Initialize val1 and val2 to the same value
uint val1 = 0b10011001;
uint val2 = 0b10011001;
// Perform XOR operation to check for identical values
uint result = val1 ^ val2;
// Output will be zero if both values are identical
Console.WriteLine(Convert.ToString(result, toBase: 2).PadLeft(8, '0'));' Initialize val1 and val2 to the same value
Dim val1 As UInteger = &B10011001UI
Dim val2 As UInteger = &B10011001UI
' Perform XOR operation to check for identical values
Dim result As UInteger = val1 Xor val2
' Output will be zero if both values are identical
Console.WriteLine(Convert.ToString(result, 2).PadLeft(8, "0"c))When he executes the XOR operation, the result is:
0000 0000
Tim explains that an XOR result of 0 means the values were identical. This makes XOR a quick way to compare two numbers for equality.
At 4:00, Tim shows another case where val1 and val2 have slight differences. This time, XOR produces a non-zero result, indicating a difference between the values.
This approach is useful when comparing large binary values efficiently because XOR can highlight differences in just one step.
Tim presents a classic programming trick: swapping two numbers without using a temporary variable.
At 5:40, he challenges viewers to swap val1 and val2 without declaring an extra variable. The solution uses XOR three times:
// Original values
uint val1 = 0b0010; // 2 in decimal
uint val2 = 0b0100; // 4 in decimal
// Swap the values using XOR
val1 = val1 ^ val2;
val2 = val1 ^ val2;
val1 = val1 ^ val2;
// After swap: val1 = 4, val2 = 2
Console.WriteLine($"val1: {val1}, val2: {val2}");' Original values
Dim val1 As UInteger = &B0010 ' 2 in decimal
Dim val2 As UInteger = &B0100 ' 4 in decimal
' Swap the values using XOR
val1 = val1 Xor val2
val2 = val1 Xor val2
val1 = val1 Xor val2
' After swap: val1 = 4, val2 = 2
Console.WriteLine($"val1: {val1}, val2: {val2}")He walks through the process:
val1 stores val1 ^ val2, which means it now holds a mix of both values.val2 is updated by XOR-ing with val1 again, leaving only the original val1 value.val1 is XOR-ed again, leaving only the original val2 value.At 7:30, Tim prints the swapped values and confirms that val1 and val2 have successfully exchanged values.
To solidify understanding, Tim runs a binary version of the swapping operation, showing how each XOR step affects the bits. He highlights that each bit flips only when necessary, making XOR an efficient way to swap values.
Tim wraps up by reinforcing the core takeaways:
He encourages viewers to experiment with XOR and apply it to real-world problems to build a deeper understanding.
Tim Corey's video provides an excellent, concise breakdown of XOR in C#. By walking through examples step-by-step, he demonstrates the operator's usefulness in comparisons, differences, and even value swapping.
If you're interested in binary operations or bitwise tricks, this is a must-watch video to strengthen your understanding of C# binary manipulation.
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!
Join our newsletter, you’ll get exclusive access on article updates. We value your privacy