I am implementing a simple IIR 2 pole low pass butterworth filter with a circular buffer of length 4 using the modulus for my wrap around when N=0 and N>4. N is an unsigned char. Example, when N=0:
k = N%4; // 0
km1 = (N-1)%4; // Wrap around to get 3
km2 = (N-2)%4; //wrap around to get 2
k, km1, km2 are used to index the y[k]'s and x[k]s. When I am debugging my code i notice that if i use the above code, the modulus is not working with the km1 and km2. They are evaluating to 255 and 254 respectively, which kills the filter.
But when I break the above into multiple lines:
k = N%4; // = 0
km1 = (N-1); //km1 = 255
km1 %= 4; //km1 = 3
km2 = (N-2) //km2 = 254
km2 %= 4; //km2 = 2
The code works, and the filter works as advertised. So my question, why is the compiler not evaluating km1 = (N-1)%4; ? Why do I have to break this up into multiple lines?