Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for(p=0;p+=(a&1)*b,a!=1;a>>=1,b<<=1);
- int peasant_mult (int a, int b) {
- for (p = 0;
- p += (a & 1) * b, a != 1;
- a /= 2, b *= 2);
- return p;}
- See? Now it's clear how the three parts of the for
- int sum = 0;
- while(1)
- {
- sum += (a & 1) * b;
- if(a == 1)
- break;
- a = a / 2;
- b = b * 2;
- }
- int sum = 0;
- while(1)
- {
- sum += (a & 1) * b;
- if(a == 1)
- break;
- a = a / 2;
- b = b * 2;
- }
- p = a * b;
- for(p=0;p+=(-(a&1))&b,a!=1;a>>=1,b<<=1);
- // Russian Peasant Multiplication ( p <- a*b, only works when a>0, b>0 )
- // See http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication
- for( p=0; p+=(a&1)*b, a!=1; a>>=1,b<<=1 );
- int RussianPeasant(int a, int b)
- {
- // sum = a * b
- int sum = 0;
- while (a != 0)
- {
- if ((a & 1) != 0)
- sum += b;
- b <<= 1;
- a >>= 1;
- }
- return sum;
- }
- function RPM(int a, int b){
- int rtn;
- for(rtn=0;rtn+=(a&1)*b,a!=1;a>>=1,b<<=1);
- return rtn;
- }
Add Comment
Please, Sign In to add comment