Python Forum
XOR solution explanation needed.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XOR solution explanation needed.
#1
Question:
For a given list of numbers, only one digit is repeated odd number of times. Pass the list through function and return the number which is repeated odd number of times.

I have a solution for it. But, I found another solution by other user which I did not understand. Here is the code

def find_it(seq):
    result = 0
    for x in seq:
        result ^= x
    return result

find_it([1,2,3,1,2,3,1])
XOR seems to be implemented but, how is the only odd number being detected.

More explanation:
I have tried printing the binary values of result and x before and after statement result ^= x to understand what is happening but, could not.
Any other resources that you can point me to get better understanding?

 For value 1 , Before ^x Operation. bin(result) =  0b0 , bin(x) =  0b1
 For value 1 , After ^x Operation. bin(result) =  0b1 , bin(x) =  0b1
Result =  1 

 For value 2 , Before ^x Operation. bin(result) =  0b1 , bin(x) =  0b10
 For value 2 , After ^x Operation. bin(result) =  0b11 , bin(x) =  0b10
Result =  3 

 For value 3 , Before ^x Operation. bin(result) =  0b11 , bin(x) =  0b11
 For value 3 , After ^x Operation. bin(result) =  0b0 , bin(x) =  0b11
Result =  0 

 For value 1 , Before ^x Operation. bin(result) =  0b0 , bin(x) =  0b1
 For value 1 , After ^x Operation. bin(result) =  0b1 , bin(x) =  0b1
Result =  1 

 For value 2 , Before ^x Operation. bin(result) =  0b1 , bin(x) =  0b10
 For value 2 , After ^x Operation. bin(result) =  0b11 , bin(x) =  0b10
Result =  3 

 For value 3 , Before ^x Operation. bin(result) =  0b11 , bin(x) =  0b11
 For value 3 , After ^x Operation. bin(result) =  0b0 , bin(x) =  0b11
Result =  0 

 For value 1 , Before ^x Operation. bin(result) =  0b0 , bin(x) =  0b1
 For value 1 , After ^x Operation. bin(result) =  0b1 , bin(x) =  0b1
Result =  1 

 For value 4 , Before ^x Operation. bin(result) =  0b1 , bin(x) =  0b100
 For value 4 , After ^x Operation. bin(result) =  0b101 , bin(x) =  0b100
Result =  5 

 For value 4 , Before ^x Operation. bin(result) =  0b101 , bin(x) =  0b100
 For value 4 , After ^x Operation. bin(result) =  0b1 , bin(x) =  0b100
Result =  1 

1

Process finished with exit code 0
Thanks,
Om
Reply


Messages In This Thread
XOR solution explanation needed. - by omm - Oct-25-2020, 06:30 AM
RE: XOR solution explanation needed. - by buran - Oct-25-2020, 07:13 AM
RE: XOR solution explanation needed. - by omm - Oct-25-2020, 08:45 AM
RE: XOR solution explanation needed. - by buran - Oct-25-2020, 09:21 AM
RE: XOR solution explanation needed. - by omm - Oct-25-2020, 01:03 PM
RE: XOR solution explanation needed. - by omm - Oct-26-2020, 06:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  New learner in Python, and need help on the term explanation BaicaiPy 3 1,329 Oct-15-2022, 03:31 PM
Last Post: Yoriz
  Some line code explanation Chrilo06 3 2,158 Feb-24-2022, 06:24 PM
Last Post: deanhystad
  While statement explanation alkhufu2 3 2,425 Sep-02-2020, 05:46 PM
Last Post: alkhufu2
  Output explanation AmanTripathi 2 2,857 Feb-14-2018, 03:03 PM
Last Post: AmanTripathi
  need help with some explanation vincelim99 2 3,665 Mar-24-2017, 04:12 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020