×

Bitwise XOR Operator in C

What is Bitwise?

As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead of numbers. Each bit has only one binary value, either 0 or 1. Most Programming languages like C, C++, Java, and Python work with 8, 16, or 32 bits, and these groups of bits is knowns as Bytes. For example, 1 Byte is combination of 8 bits.

A logical arithmetic unit known as ALU is an important part of our computer system. Multiplication, division, addition, and other mathematical operations are all carried out at the bit level inside the ALU. Bitwise operators are applied to those activities.

Bitwise XOR Operator

The Bitwise XOR operator operates on two different bits, and these two bits belong to two different numbers. These numbers are represented as the stream of binary bits.

The Caret ^ symbol in C programming stands for the bitwise XOR operator.

If both left and right operands have the same value, like both have false(0), the result of the bitwise operator will be False(0). similarly, if both left and right operands have the same value, like both have true (1), in that case, the result of bitwise will also be false(0).

On the other hand, when the two input bits vary, the bitwise XOR operator yields True (1).

As a result, XOR will produce True (1) if your input is True (1) and False (0).

Note: For the floating-point data, we cannot use bitwise XOR.

Truth Table of Bitwise XOR Operator:

Left Operand (A)Right Operand (B)Result (A ^ B)
101
011
110
000

In this case, the pair of bits Ai and Bi make up the binary sequence of the values A and B.

How does the Bitwise XOR Operator (^) Work?

Let's understand XOR operator’s working with an example. Let’s say, we have the values A=5 and B=10.

On variables A and B, we now need to use the Bitwise XOR operator. i.e., A ^ B.

  • The values of A and B must first be transformed into the binary form, or base-2 form, in order to perform the bitwise XOR.
  • We'll utilize 8 bits for the integer number to make calculations simpler. But keep in mind that the memory will only have 32 bits to record the integer value.
  • The A or 5 is equivalent to the binary value 00000101.
  • The B or 10 is equivalent to the binary value 0000 1010.
  • And now, we can infer from the Truth Table that the result will be 0 if the left operand and right operands are the same.
  • If the left operand and right operand are different, the outcome will be 1.
  • In the instance we use,
    • The binary value of the left operand is 0000 0101.
    • The binary value of the right operand is 0000 1010.

Therefore, our result in binary according to the truth table is 0000 1111, which is equal to 27 in decimal form. So, A XOR B or A^B=>5^10=27

C Program on XOR Operator

Example-1:

#include <stdio.h>
int main()
{
 int A, B, R;
 A=5;
 B=10;
//Bitwise XOR operator
R = A^B;
printf ("Result of Bitwise XOR operator using formula R = A ^ B\n");
printf("\nResult of Bitwise XOR operator is : %d\n",R);
return 0;
 
}

Explanation

We have 3 variables in the program as mentioned above: A, B, and R.

A is given the number 5.

B is given the number 10.

We have the formula R = A^B for the XOR operator.

Using the printf() function, we are displaying our Result "R" on the screen in accordance with this formula.

Output

Bitwise XOR Operator in C

Example-2: Swap two numbers using XOR:

#include<stdio.h>
int main()
{
  int A = 5, B = 10;
 
printf("Before swap: A = %d 
B =  %d\n", A, B);
  A = A ^ B;
  B = A ^ B;
  A = A ^ B;
 
printf("After swap: A = %d  B =  %d\n", A, B);
 
return 0;
}

Code Explanation:

By XORing two variables together, the following C program allows us to swap the values of two variables.

 We have A=5 and B = 10.

 We perform A = A ^ B in the first phase, outcome is 2.

This result will be assigned to A.

Then 2nd phase will start means B = A ^ B, and outcome is 5.

This result will be assigned to B.

The last phase will start means A = A ^ B, outcome is 10.

This result will be assigned to A,

Therefore, the Final assigned value to A and B will be 10 and 5, respectively.

In this way, by using XOR, we can swap two numbers.

Program Output:

Bitwise XOR Operator in C

Related Topics

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.