Re: How to count ones in a bit string?

Поиск
Список
Период
Сортировка
От Michael Wood
Тема Re: How to count ones in a bit string?
Дата
Msg-id CAP6d-HXbsJ-2rQDT2AU+ooLd-jrL1cvHpOJYJ92jMYUyqJvFpQ@mail.gmail.com
обсуждение исходный текст
Ответ на Re: How to count ones in a bit string?  (Richard Broersma <richard.broersma@gmail.com>)
Список pgsql-novice
On 21 February 2012 17:44, Richard Broersma <richard.broersma@gmail.com> wrote:
> There are probably fast better ways. But this is one way:
>
> postgres=> SELECT LENGTH( REPLACE( CAST( B'101000000000000000000010'
> AS TEXT ), '0', ''));
>  length
> --------
>      3

That seems like a good way to do it, unless you need to do this on a
lot of data and need it to be very fast or something.

If so, then maybe have a look at this:

http://en.wikipedia.org/wiki/Hamming_weight#Efficient_implementation

and maybe even this:

http://en.wikipedia.org/wiki/Hamming_weight#Processor_support

and implement it as a user defined function in C.

--
Michael Wood <esiotrot@gmail.com>

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

Предыдущее
От: Richard Broersma
Дата:
Сообщение: Re: How to count ones in a bit string?
Следующее
От: Matthias Leopold
Дата:
Сообщение: return values from plperl function