Skip to content

Instantly share code, notes, and snippets.

@senarukana
Last active August 29, 2015 14:14
Show Gist options
  • Save senarukana/a23af0af6d75cd92af47 to your computer and use it in GitHub Desktop.
Save senarukana/a23af0af6d75cd92af47 to your computer and use it in GitHub Desktop.
divide
#include <iostream>
using namespace std;
// y = 2^0 * x * a0 + 2^1 * x * a1 + ... + 2 ^ k * x * ak
int divide(unsigned x, unsigned y) {
int result = 0;
while (y >= x) {
int power = 1;
while ((x<<power) >= (x<<(power-1)) && (x<<power) <= y) {
++power;
}
result |= 1<<(power-1);
y -= x<<(power-1);
}
return result;
}
int main() {
int x = 4, y = 100;
cout<<divide(x, y)<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment