C Program Swap Two Variables using Bitwise XOR

By | March 6, 2020

C Program Swap Two Variables using Bitwise XOR

Swap Two Variables using Bitwise XOR: C Program

#include <stdio.h>
int main()
{
  
  int n1,n2;

  printf("Enter two numbers to swap values using Bitwise XOR Operator\n");
  scanf("%d%d", &n1, &n2);

  printf("Before swapping: n1=%d and n2=%d", n1,n2);
  
  n1 = n1 ^ n2;
  n2 = n1 ^ n2;
  n1 = n1 ^ n2;
  
  
  printf("\nAfter  swapping: n1=%d and n2=%d", n1,n2);
  

  return 0;
}

Output

Enter two numbers to swap values using Bitwise XOR Operator
50
100
Before swapping: n1=50 and n2=100
After swapping: n1=100 and n2=50
Press Enter to return to Quincy…

XOR (exclusive OR) operator in C Programming Language

In the C programming language, the XOR (exclusive OR) operator, represented by the symbol `^`, is a bitwise operator that performs a logical XOR operation on the individual bits of two integer operands. The XOR operation returns a value of 1 for each position where the corresponding bits in the operands are different, and 0 for each position where they are the same.

The syntax for using the XOR operator in C is:

result = operand1 ^ operand2;

Here, `operand1` and `operand2` are the integer values or variables on which the XOR operation is performed, and `result` will hold the result of the XOR operation.

The truth table for the XOR operation is as follows:

| Operand 1 | Operand 2 | Result |
|———–|———–|——–|
| 0             | 0              | 0         |
| 0             | 1               | 1         |
| 1             | 0               | 1         |
| 1             | 1               | 0         |

The XOR operator is commonly used in various programming scenarios, including:

1. **Swapping values:** XOR can be used to swap the values of two variables without using a temporary variable. This is possible because of the XOR operation’s property that applying it twice returns the original value.

a = a ^ b;
b = a ^ b;
a = a ^ b;

2.Bit manipulation: XOR is used in bit manipulation tasks like setting or clearing specific bits, toggling bits, or checking the parity of a number.

3. Data encryption and decryption: XOR operations are foundational in cryptographic algorithms. They are used to scramble and unscramble data for encryption and decryption.

4. Error detection and correction: XOR can be used to create checksums for error detection and correction in communication protocols and data storage systems.

5. Boolean algebra and logic circuits: In digital electronics, XOR gates are fundamental building blocks for implementing various logical functions.

Remember that the XOR operator works on a bit-by-bit basis, so it’s important to understand how it affects the individual bits of the operands. Additionally, while the XOR operator is versatile and has numerous applications, it should be used thoughtfully and appropriately to achieve the desired functionality in your programs.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *