Re: Bit count

Поиск
Список
Период
Сортировка
От Nathaniel Trellice
Тема Re: Bit count
Дата
Msg-id 695983.71673.qm@web25007.mail.ukl.yahoo.com
обсуждение исходный текст
Ответ на Re: Bit count  (Rikard Bosnjakovic <rikard.bosnjakovic@gmail.com>)
Список pgsql-novice
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




В списке pgsql-novice по дате отправления:

Предыдущее
От: Jasen Betts
Дата:
Сообщение: Re: Bit count
Следующее
От: "Jean-Yves F. Barbier"
Дата:
Сообщение: bytea and text