I have an array of type uint8_t
. 0 in a bit means 'OK', 1 means 'ERROR'. Each bit represents a different system.
I have to call error function if any error is detected in a bit. My solution is the following:
const int arrSize = 3;
void bitComparator() {
uint8_t myArr[arrSize] = {0, 1, 64}; //array storing system status
uint8_t mask; //bit mask
uint8_t maskResult;
for(int i=0; i<arrSize; i++) {
mask = 1; // mask is 0000 0001
for(int j=0; j<8; j++) {
maskResult = mask & myArr[i];
if(maskResult != 0) errFunc(i, j);
mask <<= 1;
}
}
If myArr
is as wide as n, then the solution is O(n)
complexity. Could you suggest any improvement on the solution regarding complexity or efficiency? It might as well be already OK, but I am not sure.
Note: I need to know the position having error.