Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::tuple<unsigned, unsigned> div(unsigned N, unsigned D) {
- unsigned Q = 0;
- unsigned R = 0;
- if(D != 0) {
- unsigned inv_N = 0;
- while(N > 0) {
- if(N % 2 == 1) {
- inv_N++;
- }
- inv_N <<= 1; // * 2
- N >>= 1; // / 2
- }
- inv_N = inv_N >> 1; // / 2
- while(inv_N > 0) {
- R <<= 1;
- if(inv_N % 2 == 1) {
- R++;
- }
- inv_N >>= 1;
- if(R >= D) {
- R = R - D;
- Q++;
- }
- Q <<= 1;
- }
- Q >>= 1;
- }
- return std::make_tuple(Q, R);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement