Skip to content

Instantly share code, notes, and snippets.

@eraserhd
Created January 29, 2013 14:41
Show Gist options
  • Save eraserhd/4664724 to your computer and use it in GitHub Desktop.
Save eraserhd/4664724 to your computer and use it in GitHub Desktop.
// Yields the approximate result of a division by 1000.
// NOTE: Can only be used for numbers less than 140,737,488,290
unsigned long quickDiv1000(unsigned long num)
{
unsigned long result;
result = num * 65 + (num >> 2) + (num >> 5) + (num >> 8)+ (num >> 11) + 32768;
return (result >> 16);
}
@ewdurbin
Copy link

working translation:

fuction: returns approximate result of division by 1000 (fucked in some edge cases)
parameters: incoming (integer)

intermediate = incoming * (2^6 + 1) + (incoming/2^1) + (incoming/2^5) + (incoming/2^8)+ (incoming/2^11) + 2^15
return (intermediate/2^16)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment