// 演算法 / Algorithm:
// 答案只和陣列長度 n 有關。n<=2 單獨處理；否則可達 XOR 值恰好是
// [0, 2^B - 1]，其中 B = n 的位元數，故答案 = 2^B。
// The answer depends only on n. Handle n<=2 specially; otherwise the
// reachable XOR values are exactly [0, 2^B - 1] with B = bit-width of n,
// so the answer is 2^B.

int uniqueXorTriplets(int* nums, int numsSize) {
    int n = numsSize;              // n 就是排列的長度 / n is the length of the permutation

    if (n == 1) return 1;          // 只有一個元素，唯一可達值就是它自己 / single element → only 1 distinct value
    if (n == 2) return 2;          // 只能得到 {1,2} 兩個值 / only {1,2} are reachable → 2 values

    // 求 n 的最高有效位的位置（index，從 0 起算）
    // Find the index of the most significant bit of n (0-based).
    int msb = 0;                   // 最高位的位置，先假設是第 0 位 / position of top bit, start at 0
    int t = n;                     // 用副本做位移，不破壞 n / a copy so we don't destroy n
    while (t > 1) {                // 只要還不止 1 個位元，就繼續往右推 / while more than one bit remains
        t >>= 1;                   // t = t / 2，砍掉最低位 / shift right by 1 = drop the lowest bit
        msb++;                     // 每砍一次，最高位位置往上加 1 / each shift raises the top-bit index
    }

    // B = msb + 1 是表示 n 所需的位元數；答案 = 2^B。
    // B = msb + 1 is the bit-width of n; the answer is 2^B.
    // 1 << (msb + 1) 就是 2 的 (msb+1) 次方，最大到 1<<17=131072，不會溢位。
    // 1 << (msb + 1) equals 2^(msb+1); at most 1<<17 = 131072, so no int overflow.
    return 1 << (msb + 1);
}
