Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Code Snippet - Special case integer division
Author:Oren
Category:Miscellaneous
File Size:528 Bytes
Uploaded at:04-May-03 05:57:58 am
Description:
  A quick way to compute A/(K*2^N) when K is odd and known in advance and A/K is known to be whole.
  
#include <stdio.h>

int oddinverse(int x)
{
 int t = x, i;
 x += 2;
 // repeat this lg(lg(MAXUINT+1))-1 times
 for (i = 4; i; i *= i)
  x *= t*x + 2;
 return -x;
}

#define A 48
#define K 3
#define N 4

void main()
{
 int C = oddinverse(K);
 // This divides A by (K*2^N) if K is odd and A/K is known to be whole.
 // It's only useful when K is known in advance, making C precomputable.
 // The method of division: Multiply by C and then shift right by N.
 printf("%d/(%d*2^%d)=%d\n", A, K, N, (A*C)>>N);
}
NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[105418]
2D Rotated Rectangles Collision Detection[78723]
Keyboard Hook[68414]
UDP[52506]
HTTP Proxy[35823]

::Top 5 Samples::
2D ColDet Rotated Rectangles[10562]
PS2 Mouse Driver[6063]
Wave Format Player[4945]
Reading FAT12[4828]
CodeGuru[4521]


All rights reserved to RageStorm © 2009