bug-gmp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C++: a >> b, where a < 0


From: Niels Rose Hansen
Subject: C++: a >> b, where a < 0
Date: Sun, 05 Jun 2005 02:07:45 +0200
User-agent: Debian Thunderbird 1.0.2 (X11/20050331)

hi,

I think the implementation of (integer) right shift is wrong for negative values of a in e.g. a >> b.

The implementation of __gmp_binary_rshift::eval( mpz... ) is based on mpz_tdiv_q_2exp, but should have been based on mpz_fdiv_q_2exp.

Example: -129 >> 8 (.... 1111 1111 0111 1111 >> 8) gives 0, whereas the correct result is -1.

Another way to put things:
For negative a, a >> b can be calculated as ~( ~a >> b), which here becomes ~( 128 >> 8) or ~0 which equals -1.

kind regards
niels rose hansen




reply via email to

[Prev in Thread] Current Thread [Next in Thread]