number of set bits

declare
  n number := ....;
  tmp number;
begin
  tmp := n - ( bitand ((n / 2) ,3681400539))
           - (bitand ((n / 4) ,1227133513));
           
   tmp := mod ( bitand ((tmp + (tmp / 8)) , 3340530119) , 63 );
  dbms_output.put_line(tmp);
end;

Google for “hakmem” to see how it works

Got some thoughts? Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: