1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<stdio.h> #include<conio.h> int main() { int a,b,c; printf ( "Read the integers from keyboard (a & b):-" ); scanf ( "%d %d" ,&a,&b); c=a^b; // Bitwise XOR(exclusive OR) // printf ( "The data after exclusive OR operation is in c=%d" ,c); getch(); } /*a=8 b=2 after execution c=10 */ |
The program reads two integers a
and b
from the user and then performs a bitwise XOR operation on these two integers. The bitwise XOR operator is represented by the symbol ^
in C programming language.
Bitwise XOR or exclusive OR (XOR) is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1.
The result of the XOR operation between a
and b
is stored in the variable c
and then displayed on the screen using the printf
function.
In the example provided, if the user inputs a=8
and b=2
, the program will perform the XOR operation as follows:
1 2 3 4 | a = 1000 ( binary ) b = 0010 ( binary ) ---------- c = 1010 ( binary ) = 10 ( decimal ) |
Thus, the program will output the result 10
on the screen.
Thanks