Implement a logical shift for mpz_t

Marc Glisse marc.glisse at
Thu May 6 14:59:51 UTC 2021

On Thu, 6 May 2021, Jan Claußen wrote:

> 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.

Might as well give the link

Do you assume that your numbers are non-negative?

> 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));
> }

No idea what that weird operation is supposed to be. mpz_mul_2exp shifts 
left, and that's that. Are you (badly) trying to emulate fixed size 

> 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?

So the functions exist, you are just confused why they are not called 

Marc Glisse

More information about the gmp-discuss mailing list