help with understanding code snippet

Hello all, I am learning arduino code and have got use to a lot of the basic stuff but sometimes when looking over other peoples code I get stuck and cannot understand what is being written, so I thought I would post it here in the hope some one could explain some.

#1 Code line's below have variable with a a mathematical some assigned to them. Why could this not just be the sum of the equation like 256 - 1 could this not be 255

``````const int EncoderResolution = 256 - 1;      // Encoder resolution 4 x <pulses per revolution>
const int MAXAttenuation = 32 - 1;          // Maximum number of attenuation steps
``````

steve8428:
#1 Code line's below have variable...

...constant...

Why could this not just be the sum of the equation like 256 - 1 could this not be 255

The author very likely intended that to mean "a power of two minus one". Presumably because the value is used as a mask.

Thanks for the reply. Forgot it should be a constant not a variable. Still not clear why 256-1 would not be the same as a constant of 255, or maybe it is the same and this is just a more clever way of writing it than I would have done.

steve8428:
Thanks for the reply. Forgot it should be a constant not a variable. Still not clear why 256-1 would not be the same as a constant of 255, or maybe it is the same and this is just a more clever way of writing it than I would have done.

It's done as a reminder that EncoderResolution must be a 8-bit having 256 values, from 0 to 256-1

The MAXAttenuation appears to be 5-bit having 32 possible values, from 0 to 32-1

Simply putting:
MAXAttenuation= 31;

could mean you are setting the attenuation to 31 out of any old range of numbers.

Other than that, the result of "32-1" is exactly the same as "31".

Yours,
TonyWilk

the result of "32-1" is exactly the same as "31".

Mathematically, this should be obvious.
What might be less obvious is that they also compile to exactly the same code on the Arduino. If you have

``````const int MAXAttenuation = 32 - 1;          // Maximum number of attenuation steps
``````

that will cause 31 to be used everywhere you mention MAXAttentuation elsewhere in the sketch, and there is no "run-time" overhead at all. (this might not be true of a BASIC program, for example.)

So it looks to me this is the same as writing the = 255 or am I still getting this wrong. Some more of the code is shown below.

``````const int EncoderResolution = 256 - 1;      // Encoder resolution 4 x <pulses per revolution>
const int MAXAttenuation = 32 - 1;          // Maximum number of attenuation steps
const int EncoderToAttenuationRatio = EncoderResolution/MAXAttenuation;
``````

If I run this in an editor with Serial.print EncoderToAttenuationRatio I get 8 which is the same as writing the following

``````const int EncoderResolution = 255;      // Encoder resolution 4 x <pulses per revolution>
const int MAXAttenuation = 31;          // Maximum number of attenuation steps
const int EncoderToAttenuationRatio = EncoderResolution/MAXAttenuation;
``````

So, at the moment I still do not understand why you would write 256 - 1. I understand this is an 8 bit number but how is this different.