I'm coding in C and I know the values that can be stored in a uint8_t
range from 0 to 255. I want to store a value that is the percentage of MAX_INTENSITY
, declared as:
#define MAX_INTENSITY 200
Can I do the following:
uint8_t my_intensity = (MAX_INTENSITY * percentage) / 100;
?
The result will be 200 or less, but during the calculation, the intermediate result can be superior to 255, e.g. with percentage = 50
, MAX_INTENSITY * percentage
= 10'000. Can this cause a problem, even if the final result will be 100, o in the range of a uint8_t
?