summaryrefslogtreecommitdiffstats
path: root/util.c
blob: efc7c14818e6b75c7f9ef9e61bd17978a9ef2d66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "util.h"

// bit population
int bitpop(uint8_t bits)
{
    int c;
    for (c = 0; bits; c++)
        bits &= bits -1;
    return c;
}

// most significant on-bit
int biton(uint8_t bits)
{
    int n = 0;
    if (bits >> 4) { bits >>= 4; n += 4;}
    if (bits >> 2) { bits >>= 2; n += 2;}
    if (bits >> 1) { bits >>= 1; n += 1;}
    return n;
}