c++ - Looking for the fastest way to divide by 2 -


i've searched half day , found interesting things using fixed point data types , bit shifting in c++ accomplish division operations while avoiding floating point math. however, have been able understand small fraction of , can't seem work.

all i'm wanting take 2 integers, ad them up, , divide 2 average. need able though, since i'm interpolating camera pixel data on arduino , have other operations do.

so i'm confused shifting in general. integer want divide 2 27. half of 27 13.5. no matter fixed point datatype try, can 13 output. example:

uint8_t  x = 27; serial.println(  x >> 1 ); 

returns 13

there's got simple way this, right?

the reason 13 because cutting off least significant bits when bit shift. since cutting them off, there no remainder check. if interested in remainder is, like:

uint8_t x = 27; serial.println((x - (x >> 1) - (x >> 1)); 

(x - (x >> 1)) should give 14 here.

it pretty simple add .5 number once determine whether remainder 1.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -