NTZ 3CW "09 March 2006" "mathcw-1.00"

Table of contents


NAME

ntz, lntz, llntz - number of trailing zero bits

SYNOPSIS

cc [ flags ] -I/usr/local/include file(s) -L/usr/local/lib -lmcw [ ... ]

#include <mathcw.h>

extern int ntz (unsigned int);

extern int lntz (unsigned long int);

extern int llntz (unsigned long long int);


DESCRIPTION

Compute the number of trailing zero bits in the integer argument. On most systems, the fast algorithm used allows this to be done in a time that is fixed, rather than proportional to the number of bits in a word. The values ntz(u) for u taken from a uniform distribution of random unsigned integers have an exponential distribution: the function value is 0 with probability 1/2 (the bits of u are xxx...1), is 1 with probability 1/4 (u is xxx...10), is 2 with probability 1/8 (u is xxx...100), and so on. The probability of having n trailing zero bits in a random integer is thus 2**(-(n+1)).

RETURN VALUES

Return the number of trailing zero bits.

ERRORS

None.

SEE ALSO

clp2(3CW), flp2(3CW), ilog2(3CW), nlz(3CW), pop(3CW). The book
Henry S. Warren, Jr. Hacker's Delight Addison-Wesley (2003) ISBN 0-201-91465-4
describes several implementations of these functions, and is a general treasure trove of clever tricks with integer arithmetic.