Bitwise XOR Operator in C Language

Spread the love

Bitwise-XOR-Operator-in-C-language-with-example-programs

Introduction:

In the previous articles, We have discussed what are Bitwise Operators and Bitwise AND Operator, and Bitwise OR operators. In today’s article, we will learn about the Bitwise XOR operator in C Language.

📢 This Article is part of the Bitwise Operators Series

Bitwise XOR Operator in C Language:

The Bitwise XOR operator is Binary Operator and Takes two input values ( Binary sequences of two values ) and performs the Bitwise OR on each pair of bits in the given binary sequence.

In C Programming, The Bitwise XOR operator is denoted by the Carot ^symbol.

The result of the Bitwise XOR operation is False(0) only if both of the input bits are the same, i.e if both inputs are True(1) then the XOR output is False(0), Similarly, if both input bits are False(0) then XOR output is False(0)

Bitwise XOR Operator returns True(1), When both input bits are different.

So if your input is True(1) and False(0), Then XOR will output True(1) and vice-versa.

We can’t apply bitwise XOR to the Floating point data.

Truth Table of Bitwise XOR Operator:

Input bit 1 ( Xi )Input bit 2 (Yi)Result ( Xi | Yi )
000
011
101
110

Here Xi and Yi are the Pair of bits in the Binary sequence of X and Y values.

How Bitwise XOR ( ‘^’ ) Operator Works?

Let’s go through a step by step example, We will take two numbers X=4 and Y=8.

Now we need to apply the Bitwise XOR operator on variables X and Y. i.e X ^ Y.

To perform the bitwise XOR, First of all, we need to convert the values of X and Y into the Binary form or Base-2 form.

➡️ For making calculations easy, We are going to take only 8 bits for the integer value. But note that the Integer value will be stored in 32 bits in the memory.

The Binary Equivalent of the X or 4 is 0000 0100.

And Binary Equivalent of the Y or 8 is 0000 1000.

The Left-most bit in the binary representation is MSB or Most significant bit and the Right-most bit is the LSB or Least Significant bit.

Now we need to apply the Bitwise OR operation on each pair of bits.

Here Pair of bits means MSB in the first number X and MSB in the second number Y, So on until we reach the LSB of X and LSB of Y.

➡️ Bitwise X ^ Y is:
                X     –>      0000 0100   (4 in Binary)
                Y     –>      0000 1000   (8 in Binary)
                X ^ Y –>     0000 1100   ( X ^ Y = 12 )

So the result of the bitwise XOR operation of X and Y ( 4 | 8 ) is 12.

Here is the graphical representation of How Bitwise XOR Operator works.

how-Bitwise-XOR-Operator-works

Let’s look at the program on the XOR operator.

Example Program 1: Bitwise XOR Operator in C Language:

In this program, we are going to perform the XOR operation on above inputs 4 and 8.

Program Output:

As excepted, The output of XOR of 4 and 8 is 12.

Let’s Take another Example:

Let’s look at another example to make our understanding concrete. We will take two numbers a = 20 and b=15.

To Perform any bitwise operation, we need to convert the decimal numbers into a binary numbers.

Binary Equivalent of the a(i.e 21) is 0001 0101

Binary Equivalent of the b(15) is 0000 1111

➡️ Bitwise A ^ B is:
                A     –>    0001 0101   (21 in Binary)
                B     –>     0000 1111    (15 in Binary)
             A ^ B –>    0001 1010   ( A ^ B = 26 )

The bitwise XOR of 21 and 15 ( 21 ^ 15 ) is 26.

 📢 For the Bitwise XOR operation, If both bits are the same output will be False(0).

Bitwise XOR Example 2:

In this program, we are going to perform the XOR operation on the above inputs 21 and 15.

Program Output:

As you can see, We got the excepted output of 26 by performing the XOR operation on 21 and 15.

Let’s look at how we can use the bitwise XOR Operator.

Invert bit using bitwise XOR Operator:

We can use the bitwise XOR operator to Invert or Toggle any bit of the given Number.

Let’s take number 10, The binary equivalent of the 10 is 1010

So our least significant bit is (Leftmost bit) index is 0. and The Rightmost bit index is 3 ( In the real world we will have 32 bits but for simplicity, we are using 4 bits)

Now we want to Invert the bit at the index 2. i.e We want to change the 2nd bit(from LSB) from 0 to 1.

Here is the logic to Invert the bit

Here we are Left shifting the ‘1’ bit times. ( 1 << 2 which becomes ‘100’ in binary )

Then we are Performing XOR operation with the above result and given number

(num ^ ‘100’) which is equal to (‘1010’ ^ ‘0100’) = ‘1110

Which gives us ‘1110’(In binary), Which is equivalent of decimal 14

Here is the C program to Invert/Toggle any bit in a given number.

Output:

This way we can Invert or Toggle any bit. The bitwise operations in the C are very fast. So whenever possible try to use the bitwise operators.

Bitiwse XOR Operator to Swap the Numbers without extra space:

Here is the logic to swap numbers using the bitwise XOR operator

The above statement is a complex statement, Which is the combination of the following statements.

Here is the explanation about above swap logic

  1. Step 1, We apply bitwise XOR on variable 'a' and variable  'b'. And assign the result to variable  'a'.
  2. Step 2: We will apply the bitwise XOR on variable  'b' and variable  'a'. And then assign the result to variable  'b'.
  3. Step 3: We will perform the bitwise XOR on variable  'a' and variable  'b'. And then assign the result to variable  'a'.
  4. After performing above three steps, The values of variable aand variable b will be swapped.

Swap two number using XOR operator Example Walkthrough:

Let’s take two variables 'a'and 'b'.
value of a=11, value of b=22.

At First step, We will perform

a=a^b;

To apply a^b, We need to have variables 'a'and 'b' values in Binary. And then we will apply the bitwise XOR on them.

  • Please note, Bitwise operators operates on the bit level, So we need to imagine the numbers in binary format to understand better.

The Binary Value of a(i.e 11) is 00001011

The Binary value of b(i.e 22) is 00010110

Now store above result to variable 'a'. So variable a become 00011101

Now let’s perform the second step b = b^a;

Store the Above (b^a) operation result into the variable 'b'. So Variable 'b' became 00001011

Now perform the third step a = a^b;

Save the result of above a^b operation to variable 'a'.

Now the variable 'a' became 00010110, Which is equal to Decimal value 22.

and the variable 'b' became 00001011, Which is equal to decimal value of 11.

As you can see the values of variable 'a' and variable 'b'are swapped. We are able to swap two numbers without using third number.

Program : Swap two numbers without using third variable ( Bitwise XOR ):

Let’s convert above XOR swap logic into the code.

Program Output:

We are using GCC compiler to compile the program on Ubuntu Linux.

📢. Learn More above compiling and running program following article – Hello World Program in C language – SillyCodes

C Tutorials Home:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

Leave a Reply