which bits are set

Sean Eric Fagan sef at kithrup.COM
Mon Dec 17 18:14:04 AEST 1990


In article <3047:Dec1618:51:1590 at kramden.acf.nyu.edu> brnstnd at kramden.acf.nyu.edu (Dan Bernstein) writes:
>The brute force (i.e., fast) approach is to use a big table.

Eek.  Why do that?  The only way I can think of to do it faster than the
presented method is to get rid of the shift and loop:

	set_bits[0] = val&1;
	set_bits[1] = val&2;
	set_bits[2] = val&4;
	/* ... */
	set_bits[31] = val&0x80000000;

Takes 32 sequential statements; on some machines, it will take 32
instructions, while on others, it might take 64.  It should not take more
than that, though (r0 <= val&mask; (set_bits+x) <= r0).

-- 
Sean Eric Fagan  | "I made the universe, but please don't blame me for it;
sef at kithrup.COM  |  I had a bellyache at the time."
-----------------+           -- The Turtle (Stephen King, _It_)
Any opinions expressed are my own, and generally unpopular with others.



More information about the Comp.lang.c mailing list