×

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

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

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...

3 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.