mpz_div_2exp function

Gutmann, A A.Gutmann at
Mon Jun 24 17:46:52 CEST 2013

Hi GMP Librarians and Gurus,

Have you seen any issues with mpz_div_2exp() function where there was a right shift with a strange rounding?  (This only happens once in a while.)

Function: void mpz_div_2exp (MP_INT *quotient, MP_INT *dividend, unsigned long int exponent_of_2)

Set quotient to dividend divided by 2 raised to exponent_of_2. This operation can also be defined as a right shift, exponent_of_2 steps, but unlike the >> operator in C, the result is rounded towards 0.
But it seems I did an mpz_div_2exp(rop, rop, rshift) // it seems to be an incorrect rounding.

  //rop   '-10000010110101011110110111110100011100111101111001' b+49 EXP=-1

mpz_div_2exp(rop, rop, 22)

  //rop   '-1000001011010101111011100000' b+27 EXP=-7

Using mpz_tdiv_2exp(rop, rop, 22) does right shift truncate just like the >> operator in C would as described above. Which I would have expected the answer to be

  //rop   '-1000001011010101111011011111' b+27 EXP=-1

This only happens once in a while. I wonder if anyone has seen something similar where mpz_div_2exp() didn't do right shift but also an incorrect rounding as well.


More information about the gmp-bugs mailing list