Doing fast leftshift stiank at
Fri Nov 11 15:47:47 CET 2005

Hi. I'm working on a very time-critical application witch includes calculations
with big integers (128bits, unsigned). Using GMP boosted the speed alot, so I
were really happy when I discovered it! 

However; I have a problem. I'm not able to do shifting of numbers in a efficient
way. As I couldn't find any methods for doing leftshift directly (including
wrapping around the edge) I created my own function for doing this – using

It goes like this : 

//shifting number given # positions and putting the result into result. 
void leftShift(mpz_t result, mpz_t number, int positions){
  //The shifting:   
  mpz_mul_2exp(result, number, positions);
  //And the wrapping:
  for(int i=0;i<positions;i++){
    if(mpz_tstbit(result, 128+i){
      mpz_clrbit(result, 128+i);
      mpz_setbit(result, i);

Any ideas about what's not good here? Are there any functions witch does this
directly witch I've missed? 

I'll be really glad for any help and suggestions!

Best Regards, 
Stian Karlsen 

More information about the gmp-discuss mailing list