Advertisement
ipsBruno

(Pawn) doAdd, doMul. Bits aritmetics operators

Oct 2nd, 2013
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 0.59 KB | None | 0 0
  1. /*
  2. *  Copyright (c) 2013
  3. *  Bruno da Silva
  4. *  Bits Mul, Add
  5. */
  6. stock doMul(x, y) {
  7.  
  8.     new p;
  9.    
  10.     for ( p = 0 ; x ;  x >>= 1 ,  y <<= 1) {
  11.    
  12.         if (x & 1) {
  13.        
  14.             for( ; y ; ) {
  15.                 p = p ^ y;
  16.                 y = (p ^ y) & y;
  17.                 y = y << 1;
  18.             }
  19.         }
  20.    }
  21.      
  22.    return p;
  23. }
  24.  
  25.  
  26. stock doAdd(a , b) {
  27.  
  28.     for( ; b ; ) {
  29.         a ^= b;
  30.         b = (a ^ b) & b;
  31.         b <<=  1;
  32.     }
  33.  
  34.     return a;
  35. }
  36.  
  37. main() {
  38.  
  39.     printf("4 x 6 = %d", doMul(4,6));  // 24
  40.     printf("4 + 6 = %d", doAdd(4,6)); // 10
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement