/*
   nb_bits.c
   création :
     Mon Jun 28 05:40:54 CEST 2004
     par whygee@f-cpu.org

   compilation :
     gcc -o nb_bits nb_bits.c

   invocation :
     nb_bits nom_du_fichier.raw
 */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  FILE *file_in;
  unsigned long long int nb_bits=0;
  unsigned long int taille=0, positif;
  double moyenne;
  signed short int sample;

  file_in=fopen(argv[1], "rb");
  if (file_in!=NULL) {
    while (
      fread(&sample, 1, sizeof(sample), file_in)
        == sizeof(sample))
    {
      taille++;

      if (sample < 0)
        positif = -sample;
      else
        positif = sample;

      while (positif != 0) {
        positif >>= 1;
        nb_bits++;
      }
    }
    if (taille > 0) {
      moyenne=nb_bits+taille;
      moyenne/=taille;
      printf("%Ld bits (%G bits/échantillon)\n",
        nb_bits,moyenne);
    }
  }
  exit(EXIT_SUCCESS);
}

