fluffy

Network Archaeology tools for Unix
git clone https://git.woozle.org/neale/fluffy.git

Neale Pickett  ·  2018-10-15

entropy.c

 1#include <stdio.h>
 2#include <math.h>
 3
 4int
 5main(int argc, char *argv[])
 6{
 7	size_t len = 0;
 8	size_t hist[256] = {0};
 9	int c;
10    int i;
11
12	while ((c = getchar()) != EOF) {
13		hist[c] += 1;
14		len += 1;
15	}
16
17	float H = 0;
18	for (i = 0; i < 256; i += 1) {
19		if (hist[i]) {
20			float Hi = (float)hist[i]/len;
21			H -= Hi * log2(Hi);
22		}
23	}
24
25	printf("%f\n", H);
26    return 0;
27}
28