find position of most significant bit ?

Karl Hasselström
Wed, 27 Nov 2002 09:51:17 +0100

On 2002-11-26 16:25:18 -0500, David T. Ashley wrote:
> I assume one knows from the data structure of an arbitrary integer
> where the limb containing the most significant bit is.

Yes. If A is an mpz_t, then A->_mp_d[ABS(A->_mp_size) - 1] is the most
significant limb, since ABS(A->_mp_size) is the number of limbs and
the most significant limb is always nonzero. I'm not exactly sure what
happens when A is zero, but I think that ABS(A->_mp_size) is zero
then, so there are no limbs. (Someone correct me if I'm wrong!)

> Then the problem reduces to the problem of finding the most
> significant within a limb (a machine word).

In longlong.h there is a macro called count_leading_zeros that does

Kalle Hasselström,