Thanks for all the replies.
I'm giving this a spin, at present. It's based on Rikard's suggestion of using plpgsql script function. Like Jason and
otherhave said, it could be done in a C function (and this was my first inclination), but the hassle of worrying about
linkinglibraries, 32/64-bit installation etc is probably overkill. So this implementation of the 'MIT Hakmem' algorithm
seemedlike a good compromise between efficiency and efficacy:
CREATE FUNCTION mybitcount(n int4) RETURNS int4 AS $$
DECLARE tmp int4;
BEGIN
tmp = n - ((n >> 1) & 3681400539) - ((n >> 2) & 1227133513);
RETURN ((tmp + (tmp >> 3)) & 3340530119) % 63;
END
$$ LANGUAGE plpgsql IMMUTABLE STRICT;
Nathaniel