I want to perform not operation to a block of continous elements of a bool array and then read back the complete array. I am using the following code to perform the operation.
bool arr[100000]={0};
cin>>x>>y;
for(i=x; i<=y; i++)
arr[i]=!arr[i];
//Some other operations on the array
for(i=0; i<=100000; i++)
arr+=arr[i];
This works fine but i am trying to increase the speed of the program. Is there a better way to perform the same operation?
Since I made the comment about using ints (or indeed int64), I may as well write it up and you can evaluate whether it's worth it. It would be something like this. Forgive any errors, as I'm just bunging this into a browser while my kids are watching ridiculously trashy saturday-morning cartoons.
Alternative for the above loop, if it makes any difference...
Exercise is to try with 64-bit integers. Personally, I reckon this approach would be faster than anything else except in the cases where you are only flipping a few bits.
I might have an off-by-one-bit error in the right-hand mask. If anyone spots it please comment. Brain empty. =)
Consider to use bitset. Compare performance - maybe it will be better.
For even more optimized (please measure and don't believe) you can use your own version of bitset<>, THIS IS NOT TESTED CODE: