Setting and clearing multiple bits

Roberto Bagnara bagnara at
Thu Sep 4 10:43:13 CEST 2008

Hi there,

I need to efficiently implement two functions:

   //! Sets the bits of \p x up to position \p k (included).
   void set_until(mpz_t x, unsigned long k);

   //! Clears the bits of \p x from position \p k (included) onward.
   void clear_from(mpz_t x, unsigned long k);

For the secone one, I currently have:

clear_from(mpz_t x, unsigned long k) {
   mpz_tdiv_r_2exp(x, x, k);

Is this the best I can do?  And how could I efficiently
compute set_until()?
All the best,


Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
mailto:bagnara at

More information about the gmp-discuss mailing list