-2

i know that xor operator returns true if both its input differ but with this knowledge i cant understand various coding problem across the internet. like these:

https://www.hackerearth.com/february-easy-16/algorithm/utkarsh-and-sub-array-xor-february-easy/

https://www.hackerrank.com/contests/hourrank-5/challenges/xor-se

pls help me understand these. btw i use c++ for coding. explaining how to use XOR operator in these will be enough, no need to explain the full question.

Lcukerd
  • 71
  • 7

2 Answers2

3

An addition modulo 2 is equivalent to XOR.

0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

equals to

( 0 + 0 ) mod 2 = 0
( 0 + 1 ) mod 2 = 1
( 1 + 0 ) mod 2 = 1
( 1 + 1 ) mod 2 = 0

Now you could use mod to solve XOR stuff.

Johannes
  • 93
  • 8
0

There is no logical opertor xor in C/C++ and many other languages, only bitwise. It works separately for each corresponding bits of operands in there binary form.

Semyon Burov
  • 1,090
  • 1
  • 9
  • 15