Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. std::tuple<unsigned, unsigned> div(unsigned N, unsigned D) {
  2.     unsigned Q = 0;
  3.     unsigned R = 0;
  4.  
  5.     if(D != 0) {
  6.  
  7.  
  8.     unsigned inv_N = 0;
  9.  
  10.  
  11.     while(N > 0) {
  12.         if(N % 2 == 1) {
  13.             inv_N++;            
  14.         }
  15.         inv_N <<= 1; // * 2
  16.         N >>= 1; // / 2
  17.     }
  18.     inv_N = inv_N >> 1; // / 2
  19.  
  20.     while(inv_N > 0) {
  21.         R <<= 1;
  22.  
  23.         if(inv_N % 2 == 1) {
  24.             R++;            
  25.         }
  26.  
  27.         inv_N >>= 1;
  28.  
  29.         if(R >= D) {
  30.             R = R - D;
  31.             Q++;
  32.         }
  33.         Q <<= 1;
  34.     }
  35.  
  36.     Q >>= 1;
  37.     }
  38.  
  39.     return std::make_tuple(Q, R);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement