If I have the following function:(adsbygoogle = window.adsbygoogle || []).push({});

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!

**Physics Forums - The Fusion of Science and Community**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Arithmetic shift divison question

Loading...

Similar Threads - Arithmetic shift divison | Date |
---|---|

C/++/# Is my code suffering from an arithmetic error? | Feb 25, 2017 |

Insights Why Can't My Computer Do Simple Arithmetic? - Comments | Jan 29, 2016 |

Arithmetic Issue in FORTRAN | Oct 28, 2015 |

Arithmetic Issue in FORTRAN | Oct 17, 2015 |

[Win32 API, C] Bit shifting | Dec 24, 2014 |

**Physics Forums - The Fusion of Science and Community**