Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Last active August 29, 2015 14:12
Show Gist options
  • Save yohhoy/54e7d8d51dda1fbe3822 to your computer and use it in GitHub Desktop.
Save yohhoy/54e7d8d51dda1fbe3822 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <assert.h>
void div(int n, int d)
{
int m = 1, q = 0;
assert(0 <= n && 0 < d);
while (d <= n) {
d <<= 1;
m <<= 1;
}
while (1 < m) {
d >>= 1;
m >>= 1;
if (n >= d) {
n -= d;
q |= m;
}
}
printf("%d,%d\n", q, n);
}
int main()
{
div(0, 3); // 0,0
div(10, 3); // 3,1
div(10, 4); // 2,2
div(10, 5); // 2,0
div(10, 6); // 1,4
div(10, 10); // 1,0
div(10, 11); // 0,10
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment