OpenCores
URL https://opencores.org/ocsvn/pcounter/pcounter/trunk

Subversion Repositories pcounter

[/] [pcounter/] [trunk/] [pcount.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 robotron
#include <stdlib.h>
2
#include <string.h>
3
#include <stdio.h>
4
#include <ctype.h>
5
 
6
typedef unsigned long count_t;
7
 
8
count_t pcount_decode(unsigned n, count_t b) {
9
  count_t y = b;
10
  unsigned k;
11
  for (k = 1; k < n; k++)
12
    if ((y & ((1<<k)-1)) < k)
13
      y = y ^ (1<<k);
14
  return y;
15
}
16
 
17
count_t pcount_encode(unsigned n, count_t y) {
18
  count_t b = y;
19
  unsigned k;
20
  for (k = n-1; k > 0; k--)
21
    if ((b & ((1<<k)-1)) < k)
22
      b = b ^ (1<<k);
23
  return b;
24
}
25
 
26
int main() {
27
  char s[256], *c;
28
  count_t b, y, z;
29
  for (;;) {
30
    if (!fgets(s, sizeof(s), stdin))
31
      break;
32
    for (c = s + strlen(s) - 1; isspace(*c); c--)
33
      *c = '\0';
34
 
35
    b = strtoul(s, NULL, 2);
36
    y = pcount_decode(strlen(s), b);
37
    z = pcount_encode(strlen(s), y);
38
    printf("%08lx -> %08lx -> %08lx\n", b, y, z);
39
  }
40
  return 0;
41
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.