# mpz_div_2exp function

Gutmann, A A.Gutmann at analog.com
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.

Thanks.
Regards,
A

```
{"ft_lang_label":"__label__en","ft_lang_prob":0.8532098,"math_prob":0.47722006,"size":1249,"snap":"2020-10-2020-16","text_gpt3_token_len":363,"char_repetition_ratio":0.12771085,"word_repetition_ratio":0.010928961,"special_character_ratio":0.3418735,"punctuation_ratio":0.12227074,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9591398,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-03-29T12:48:53Z\",\"WARC-Record-ID\":\"<urn:uuid:269e311c-b231-4027-a039-f79dd062da4f>\",\"Content-Length\":\"3768\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b6374a9d-8a68-4d76-ba2b-4276b2b0e0c1>\",\"WARC-Concurrent-To\":\"<urn:uuid:87fd8200-2a85-4959-be0c-4d39747ae2a7>\",\"WARC-IP-Address\":\"130.242.124.102\",\"WARC-Target-URI\":\"https://gmplib.org/list-archives/gmp-bugs/2013-June/003087.html\",\"WARC-Payload-Digest\":\"sha1:6XF6Y3GYMYUFP4VRYY5TXHHB6ZJ2AOGN\",\"WARC-Block-Digest\":\"sha1:D3M5NMOTW6XLSWTVRZGX7T3P6647XDKV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585370494331.42_warc_CC-MAIN-20200329105248-20200329135248-00429.warc.gz\"}"}