Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. int myXOR(int x, int y)
  2. {
  3. int res = 0; // Initialize result
  4.  
  5. // Assuming 32-bit Integer
  6. for (int i = 31; i >= 0; i--)
  7. {
  8. // Find current bits in x and y
  9. bool b1 = x & (1 << i);
  10. bool b2 = y & (1 << i);
  11.  
  12. // If both are 1 then 0 else xor is same as OR
  13. bool xoredBit = (b1 & b2) ? 0 : (b1 | b2);
  14.  
  15. // Update result
  16. res <<= 1;
  17. res |= xoredBit;
  18. }
  19. return res;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement