XOR Operation in an Array

easy bit manipulation math

Problem

You are given two integers n and start. Define an array nums of length n where nums[i] = start + 2*i for each index i from 0 to n-1. Return the bitwise XOR of every element in nums.

Inputn = 5, start = 0
Output8
nums = [0, 2, 4, 6, 8], and 0 ⊕ 2 ⊕ 4 ⊕ 6 ⊕ 8 = 8.

def xor_operation(n, start):
    acc = 0
    for i in range(n):
        acc ^= start + 2 * i
    return acc
function xorOperation(n, start) {
  let acc = 0;
  for (let i = 0; i < n; i++) {
    acc ^= start + 2 * i;
  }
  return acc;
}
class Solution {
    public int xorOperation(int n, int start) {
        int acc = 0;
        for (int i = 0; i < n; i++) {
            acc ^= start + 2 * i;
        }
        return acc;
    }
}
int xorOperation(int n, int start) {
    int acc = 0;
    for (int i = 0; i < n; i++) {
        acc ^= start + 2 * i;
    }
    return acc;
}
Time: O(n) Space: O(1)