xoring a slice in Go

Viewed 6201

So I'm trying to xor random, which is 4 bytes, with every four bytes of something. The thing is, I want to change random to ans (which is the result of the xor) and then keep going. So basically the random variable will be a fixed number the first time but will change afterwards until the loop ends. My code seems to have the correct logic, however, I keep getting (operator ^ not defined on slice)

random := 4 bytes
for j:=0;j<len(something);j+=4{
ans:=something[j:j+4] ^ random
random=ans
}

My guess is, slice doesn't allow xor, and something will have to be slice since I'm kind of slicing the array into multiple bytes. Any idea how to solve this problem?

1 Answers

The application should xor the individual bytes. Something like this:

var random [4]byte
for i, b := range something {
    random[i&3] ^= b  // xor b on element of random
}

This sets

random[0] = random[0] ^ something[0] ^ something[4] ....
random[1] = random[1] ^ something[1] ^ something[5] ...
... and so on
Related