×

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

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

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

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.