- #1
bitrex
- 193
- 0
If I have the following function:
a = b * c/255
The following function is apparently equivalent using only shifts:
product = b * c;
a = (product + (product>>8) + 1)>>8;
I am having trouble following how this function works. Since an arithmetic right shift is division by a power of 2 right shift 8 would be division by 256...somehow (product + (product>>8) +1) is compensating for the difference between the two but I'm not sure why it's that and not just (product + 1). I'm too thickheaded to see it, any clarification would be appreciated!
a = b * c/255
The following function is apparently equivalent using only shifts:
product = b * c;
a = (product + (product>>8) + 1)>>8;
I am having trouble following how this function works. Since an arithmetic right shift is division by a power of 2 right shift 8 would be division by 256...somehow (product + (product>>8) +1) is compensating for the difference between the two but I'm not sure why it's that and not just (product + 1). I'm too thickheaded to see it, any clarification would be appreciated!