publicclassSolution { publicintXorOperation(int n, int start) { int s = start >> 1, e = n & start & 1; int ret = SumXor(s - 1) ^ SumXor(s + n - 1); return ret << 1 | e; }
publicintSumXor(int x) { if (x % 4 == 0) { return x; } if (x % 4 == 1) { return1; } if (x % 4 == 2) { return x + 1; } return0; } }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var xorOperation = function(n, start) { let s = start >> 1, e = n & start & 1; let ret = sumXor(s - 1) ^ sumXor(s + n - 1); return ret << 1 | e; };
constsumXor = (x) => { if (x % 4 === 0) { return x; } if (x % 4 === 1) { return1; } if (x % 4 === 2) { return x + 1; } return0; }
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
funcsumXor(x int)int { switch x % 4 { case0: return x case1: return1 case2: return x + 1 default: return0 } }
funcxorOperation(n, start int) (ans int) { s, e := start>>1, n&start&1 ret := sumXor(s-1) ^ sumXor(s+n-1) return ret<<1 | e }
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
intsumXor(int x) { if (x % 4 == 0) { return x; } if (x % 4 == 1) { return1; } if (x % 4 == 2) { return x + 1; } return0; }
intxorOperation(int n, int start) { int s = start >> 1, e = n & start & 1; int ret = sumXor(s - 1) ^ sumXor(s + n - 1); return ret << 1 | e; }