Total number of "1s" in a Postgres bitmask
# select length(replace(x::text, '0', '')) from ( values ('1010111101'::bit varying) ) as something(x);
length
--------
7
(1 row)
And approach without string conversion:
# select count(*) from ( select x, generate_series(1, length(x)) as i from ( values ('1010111101'::bit varying) ) as something(x) ) as q where substring(x, i, 1) = B'1';
count
-------
7
(1 row)
If you need it to be really efficient, here's a discussion: Efficiently determining the number of bits set in the contents of, a VARBIT field