I have two arrays of booleans, both have the same known size.
I would like to perform a XOR on the arrays and store the result in a third boolean array c.
I could iterate on the arrays and do a XOR on each value, but I was wondering if there was a faster way to do it, since it is a very "low level" operation?
Bottom line : whats the fastest way to XOR two boolean arrays?
Thanks,
Cyp
1 2 3 4 5 6 7
#what i am doing for now:
bool a[100];
bool b[100];
bool c[100];
for(int i=0; i<100; i++){
c[i]=a[i]^b[i];
}
I imagine the compiler would optimise it for you. Just compile with optimisations turn on :) That should also enable look-unrolling which would make it even faster.
He could be trying to compile this on some strange hardware (SPARC, MIPS, RISC).
As I said. The compiler will likely optimise. If you wanted to make it easier for the compiler, unroll you loops to 5->10 instructions so it can execute concurrently.
Well, yeah. That's why my comment states that the code assumes sizeof(long)==4. It was just an example. I took advantage of the fact that !(100%4) and that sizeof(long)==4 is very common.
An interesting fact: the loop will never produce a buffer overflow, so if (n%sizeof(long)), it's always possible to complete the last steps with the bool pointer.
Another solution would be to use int32_t, although that's not quite standard, sadly.
I believe that C defines "int" to be the word size of the architecture, so perhaps helios' solution with "int" instead of "long" accommodates Zaita's point.
For that last ounce of speed, I'd also try to write the loop such that the terminating condition was i == 0, since comparison to zero can be slightly faster than comparing to nonzero, and I'd also use pre-increment/pre-decrement instead of post-.