5 ms·
Yes. this is doing to infamous division by 10 using bit shifts as you will find in hacker's delight: http://www.hackersdelight.org/divcMore.pdf http://www.hacke
by waynecochran 9y ago
Yes. this is doing to infamous division by 10 using bit shifts as you will find in hacker's delight:
http://www.hackersdelight.org/divcMore.pdf http://www.hackersdelight.org/divcMore.pdf.
You may notice that when you divide num by 10 you get a quotient q and a remainder r:
num = q*10 + r
Once you get q, you can solve for r as
r = num - q*10
So this is how you get r and q:
q = (num >> 1) + (num >> 2);
q = q + (q >> 4);
q = q + (q >> 8);
q = q + (q >> 16);
q = q >> 3;
r = num - q*10;
q = q + ((r + 6) >> 4)
voila!
- kwillets 9y agoYep, 205/2048 = .10009.. This is something I'm actually using, as I'm serializing small int's (0-99). For those it all fits in 16 bits.