Implement a logical shift for mpz_t

Jan Claußen jan.claussen10 at web.de
Thu May 6 12:00:51 UTC 2021


Hi,

I am just fiddling around with mpz_t variables and it took me quite a long time to come up with a bit shifting method with some help of the Stackexchange hive mind.
These are the functions I have now:
inline void mpz_lshift(mpz_t rop, mpz_t op1, mp_bitcnt_t op2) {
mpz_mul_2exp(rop,op1,op2);
mpz_tdiv_r_2exp(rop, rop, mpz_popcount(op1));
}

inline void mpz_rshift(mpz_t rop, mpz_t op1, mp_bitcnt_t op2) {
mpz_fdiv_q_2exp(rop, op1, op2);
}

Is this a practical solution? There is probably a better way to do this in assembly, but it works for now. I just wonder why no functions like this exist yet. Is there a good reason?
Cheers,
Jan



More information about the gmp-discuss mailing list