Intelligent use of XOR operator to find missing number

I’ve come across the following problem on leetcode & tried to solve it with the following code however there seems to be an even better solution that takes advantage of XOR. Leetcode has a description for XOR solution however I can’t grasp it in its entirety.

I just can’t wrap my head around why we need to initialize missing variable with length of array, why not initialize it with Zero & when we do initialize it with Zero, why can’t this algo find the missing number? Can someone please explain it?

enter image description here

Following was my solution before I learned Leetcode suggested XOR solution is even better & faster

class Solution {     public int missingNumber(int[] nums) {         if(nums.length == 1)             return nums[0] == 0 ? 1 : 0;          int missing = 0;         boolean tempNums[] = new boolean[nums.length+1]; //cuz one is missing          for(int i:nums) {             tempNums[i] = true;         }          for(int i=0; i<tempNums.length; i++)             if(!tempNums[i]) {                 missing = i;                 break;             }         return missing;     } }