Bitwise logic gmpxx.h problems

Marc Glisse marc.glisse at
Fri Jul 25 18:47:31 CEST 2008

On Fri, 25 Jul 2008, joppe.bos at wrote:

> As far as I can tell the operator&=(double) gets indeed forwarded to 
> operator&=(unsigned long) but I think, I am not 100% sure, this is done by 
> casting the value. In other words it gets casted (truncated) and this is 
> exactly what mpz_set_d does.

When the integer represented by the double is larger than an unsigned long 
can represent, mpz_set_d does something different I believe. Whether 
allowing double arguments in bit operations is useful, there I have 
doubts, but it seems best to make it equivalent to first casting to 
mpz_class if we allow it.

> If this is true then the current situation works 
> perfectly. I have done multiple tests and never found any strange behavior 
> (but this obviously doesn't mean it works).

Pick a large double like d=1e300, create mpz_class z=d, and then try z&=d. 
I would prefer if this kept z intact instead of setting it to 0 as the 
unsigned long version probably does.

> If one wants to change this behavior in order to avoid confusion I opt for 
> "yet another overload for the eval method". But then we need to do this as 
> well for all the three functions: operator^=, operator|= and operator&=. 
> Implementing it only for "double" is very straightforward; just reuse the 
> code in the mail from Torbjorn and replace the "unsigned long int i" with 
> "double i" and the mpz_set_ui with a mpz_set_d statement.

And then you also need the signed long overload as in most other 

Marc Glisse

More information about the gmp-bugs mailing list