reversing a mask

hamilton at uiucuxc.UUCP hamilton at uiucuxc.UUCP
Wed Dec 12 09:30:00 AEST 1984


i'm straying even further from the subject, but eryk vershen's note
reminded me of some cute code i found somewhere long ago:

    /* onebits - count 1-bits in an int
     * using Reingold, Nievergelt & Deo's Combinatorial Algorithm #1.3.
     */

    static unsigned long int Hi[5] =
    { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };

    static unsigned long int Lo[5] =
    { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF };

    onebits (n)
	register unsigned long int n;
    {
	register int shift;
	register unsigned long int *H = Hi, *L = Lo;

	for (shift = 1; shift <= 16; shift *= 2)
	    n = ((n & *H++) >> shift) + (n & *L++);
	return (n);
    }

wayne ({decvax,ucbvax}!pur-ee!uiucdcs!uiucuxc!)hamilton



More information about the Comp.lang.c mailing list