Find XOR of two number without using XOR operator | Java program to find XOR without using ^ ; Returns XOR of x and y ; Initialize result ; Assuming 32 - bit Integer ; Find current bits in x and y ; If both are 1 then 0 else xor is same as OR ; Update result ; Driver Code
import java . io . * ; class GFG { static int myXOR ( int x , int y ) { int res = 0 ; for ( int i = 31 ; i >= 0 ; i -- ) { int b1 = ( ( x & ( 1 << i ) ) == 0 ) ? 0 : 1 ; int b2 = ( ( y & ( 1 << i ) ) == 0 ) ? 0 : 1 ; int xoredBit = ( ( b1 & b2 ) != 0 ) ? 0 : ( b1 b2 ) ; res <<= 1 ; res |= xoredBit ; } return res ; } public static void main ( String [ ] args ) { int x = 3 , y = 5 ; System . out . println ( " XOR ▁ is ▁ " + myXOR ( x , y ) ) ; } }