1/* @title: Bit operations */
2#pragma once
3#include <stddef.h>
4
5static inline size_t popcount(size_t n) {
6 size_t count = 0;
7 while (n > 0) {
8 if (n & 1)
9 count++;
10
11 n >>= 1;
12 }
13 return count;
14}
15
16static inline size_t next_pow2(size_t x) {
17 size_t p = 1;
18 if (x == 0)
19 return 1;
20 while (p < x) {
21 p <<= 1;
22 }
23 return p;
24}
25
26static inline size_t prev_pow2(size_t x) {
27 size_t p = 1;
28 if (x == 0)
29 return 1;
30 while (p <= x) {
31 p <<= 1;
32 }
33 return p >> 1;
34}
35