-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbits_count.sql
More file actions
33 lines (31 loc) · 818 Bytes
/
bits_count.sql
File metadata and controls
33 lines (31 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
CREATE OR REPLACE FUNCTION bits_count(value bigint) RETURNS integer AS $$
DECLARE i integer;
c integer;
bits BIT(25);
BEGIN
c := 0;
bits := value::BIT(25);
FOR i IN 1..LENGTH(bits) LOOP
IF substring(bits, i, 1) = B'1' THEN
c := c + 1;
END IF;
END LOOP;
RETURN c;
END;
$$ LANGUAGE plpgsql;
-- another one to accept only integer (coming from python calls mainly)
CREATE OR REPLACE FUNCTION bits_count(value integer) RETURNS integer AS $$
DECLARE i integer;
c integer;
bits BIT(25);
BEGIN
c := 0;
bits := value::BIT(25);
FOR i IN 1..LENGTH(bits) LOOP
IF substring(bits, i, 1) = B'1' THEN
c := c + 1;
END IF;
END LOOP;
RETURN c;
END;
$$ LANGUAGE plpgsql;