Wednesday, 23 December 2015

Program to find XOR without using XOR operator

1. Find bitwise or of X and Y, then result will have set bits where either X or Y have set bits. Eg: X = 3(011), Y=5(101) their or will be 7(111).

2. Now we find places where both X and Y have set bits. The value of expression "~X | ~Y" has 0 bits wherever X and Y both have set bits.

3. Bitwise and of "X|Y" and "~X | ~Y" produces the required result.


#include <stdio.h>

int findXOR(int x, int y)
{
   return (x | y) & (~x | ~y);
}
 
int main()
{
   int x,y;
   printf("Enter x and y: ");
   scanf("%d %d",&x,&y);
   printf("XOR is findXOR(x,y)");
   return 0; 
}



No comments:

Post a Comment