fork download
  1. /* package whatever; // don't place package name! */
  2. /*
  3.  
  4.   This source code accompanies the article, "Using The Golay Error Detection And
  5.   Correction Code", by Hank Wallace. This program demonstrates use of the Golay
  6.   code.
  7.   Usage: G DATA Encode/Correct/Verify/Test
  8.   where DATA is the data to be encoded, codeword to be corrected,
  9.   or codeword to be checked for errors. DATA is hexadecimal.
  10.   Examples:
  11.   G 555 E encodes information value 555 and prints a codeword
  12.   G ABC123 C corrects codeword ABC123
  13.   G ABC123 V checks codeword ABC123 for errors
  14.   G ABC123 T tests routines, ABC123 is a dummy parameter
  15.   This program may be freely incorporated into your programs as needed. It
  16.   compiles under Borland's Turbo C 2.0. No warranty of any kind is granted.
  17.   */
  18. #include "stdio.h"
  19. #include "conio.h"
  20. #define POLY 0xAE3 /* or use the other polynomial, 0xC75 */
  21.  
  22. /* ====================================================== */
  23.  
  24. unsigned long golay(unsigned long cw)
  25. /* This function calculates [23,12] Golay codewords.
  26.   The format of the returned longint is
  27.   [checkbits(11),data(12)]. */
  28. {
  29. int i;
  30. unsigned long c;
  31. cw&=0xfffl;
  32. c=cw; /* save original codeword */
  33. for (i=1; i<=12; i++) /* examine each data bit */
  34. {
  35. if (cw & 1) /* test data bit */
  36. cw^=POLY; /* XOR polynomial */
  37. cw>>=1; /* shift intermediate result */
  38. }
  39. return((cw<<12)|c); /* assemble codeword */
  40. }
  41.  
  42. /* ====================================================== */
  43.  
  44. int parity(unsigned long cw)
  45. /* This function checks the overall parity of codeword cw.
  46.   If parity is even, 0 is returned, else 1. */
  47. {
  48. unsigned char p;
  49.  
  50. /* XOR the bytes of the codeword */
  51. p=*(unsigned char*)&cw;
  52. p^=*((unsigned char*)&cw+1);
  53. p^=*((unsigned char*)&cw+2);
  54.  
  55. /* XOR the halves of the intermediate result */
  56. p=p ^ (p>>4);
  57. p=p ^ (p>>2);
  58. p=p ^ (p>>1);
  59.  
  60. /* return the parity result */
  61. return(p & 1);
  62. }
  63.  
  64. /* ====================================================== */
  65.  
  66. unsigned long syndrome(unsigned long cw)
  67. /* This function calculates and returns the syndrome
  68.   of a [23,12] Golay codeword. */
  69. {
  70. int i;
  71. cw&=0x7fffffl;
  72. for (i=1; i<=12; i++) /* examine each data bit */
  73. {
  74. if (cw & 1) /* test data bit */
  75. cw^=POLY; /* XOR polynomial */
  76. cw>>=1; /* shift intermediate result */
  77. }
  78. return(cw<<12); /* value pairs with upper bits of cw */
  79. }
  80.  
  81. /* ====================================================== */
  82.  
  83. int weight(unsigned long cw)
  84. /* This function calculates the weight of
  85.   23 bit codeword cw. */
  86. {
  87. int bits,k;
  88.  
  89. /* nibble weight table */
  90. const char wgt[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
  91.  
  92. bits=0; /* bit counter */
  93. k=0;
  94. /* do all bits, six nibbles max */
  95. while ((k<6) && (cw))
  96. {
  97. bits=bits+wgt[cw & 0xf];
  98. cw>>=4;
  99. k++;
  100. }
  101.  
  102. return(bits);
  103. }
  104.  
  105. /* ====================================================== */
  106.  
  107. unsigned long rotate_left(unsigned long cw, int n)
  108. /* This function rotates 23 bit codeword cw left by n bits. */
  109. {
  110. int i;
  111.  
  112. if (n != 0)
  113. {
  114. for (i=1; i<=n; i++)
  115. {
  116. if ((cw & 0x400000l) != 0)
  117. cw=(cw << 1) | 1;
  118. else
  119. cw<<=1;
  120. }
  121. }
  122.  
  123. return(cw & 0x7fffffl);
  124. }
  125.  
  126. /* ====================================================== */
  127.  
  128. unsigned long rotate_right(unsigned long cw, int n)
  129. /* This function rotates 23 bit codeword cw right by n bits. */
  130. {
  131. int i;
  132.  
  133. if (n != 0)
  134. {
  135. for (i=1; i<=n; i++)
  136. {
  137. if ((cw & 1) != 0)
  138. cw=(cw >> 1) | 0x400000l;
  139. else
  140. cw>>=1;
  141. }
  142. }
  143.  
  144. return(cw & 0x7fffffl);
  145. }
  146.  
  147. /* ====================================================== */
  148.  
  149. unsigned long correct(unsigned long cw, int *errs)
  150. /* This function corrects Golay [23,12] codeword cw, returning the
  151.   corrected codeword. This function will produce the corrected codeword
  152.   for three or fewer errors. It will produce some other valid Golay
  153.   codeword for four or more errors, possibly not the intended
  154.   one. *errs is set to the number of bit errors corrected. */
  155. {
  156. unsigned char
  157. w; /* current syndrome limit weight, 2 or 3 */
  158. unsigned long
  159. mask; /* mask for bit flipping */
  160. int
  161. i,j; /* index */
  162. unsigned long
  163. s, /* calculated syndrome */
  164. cwsaver; /* saves initial value of cw */
  165.  
  166. cwsaver=cw; /* save */
  167. *errs=0;
  168. w=3; /* initial syndrome weight threshold */
  169. j=-1; /* -1 = no trial bit flipping on first pass */
  170. mask=1;
  171. while (j<23) /* flip each trial bit */
  172. {
  173. if (j != -1) /* toggle a trial bit */
  174. {
  175. if (j>0) /* restore last trial bit */
  176. {
  177. cw=cwsaver ^ mask;
  178. mask+=mask; /* point to next bit */
  179. }
  180. cw=cwsaver ^ mask; /* flip next trial bit */
  181. w=2; /* lower the threshold while bit diddling */
  182. }
  183.  
  184. s=syndrome(cw); /* look for errors */
  185. if (s) /* errors exist */
  186. {
  187. for (i=0; i<23; i++) /* check syndrome of each cyclic shift */
  188. {
  189. if ((*errs=weight(s)) <= w) /* syndrome matches error pattern */
  190. {
  191. cw=cw ^ s; /* remove errors */
  192. cw=rotate_right(cw,i); /* unrotate data */
  193. return(s=cw);
  194. }
  195. else
  196. {
  197. cw=rotate_left(cw,1); /* rotate to next pattern */
  198. s=syndrome(cw); /* calc new syndrome */
  199. }
  200. }
  201. j++; /* toggle next trial bit */
  202. }
  203. else
  204. return(cw); /* return corrected codeword */
  205. }
  206.  
  207. return(cwsaver); /* return original if no corrections */
  208. } /* correct */
  209.  
  210. /* ====================================================== */
  211.  
  212. int decode(int correct_mode, int *errs, unsigned long *cw)
  213. /* This function decodes codeword *cw in one of two modes. If correct_mode
  214.   is nonzero, error correction is attempted, with *errs set to the number of
  215.   bits corrected, and returning 0 if no errors exist, or 1 if parity errors
  216.   exist. If correct_mode is zero, error detection is performed on *cw,
  217.   returning 0 if no errors exist, 1 if an overall parity error exists, and
  218.   2 if a codeword error exists. */
  219. {
  220. unsigned long parity_bit;
  221.  
  222. if (correct_mode) /* correct errors */
  223. {
  224. parity_bit=*cw & 0x800000l; /* save parity bit */
  225. *cw&=~0x800000l; /* remove parity bit for correction */
  226.  
  227. *cw=correct(*cw, errs); /* correct up to three bits */
  228. *cw|=parity_bit; /* restore parity bit */
  229.  
  230. /* check for 4 bit errors */
  231. if (parity(*cw)) /* odd parity is an error */
  232. return(1);
  233. return(0); /* no errors */
  234. }
  235. else /* detect errors only */
  236. {
  237. *errs=0;
  238. if (parity(*cw)) /* odd parity is an error */
  239. {
  240. *errs=1;
  241. return(1);
  242. }
  243. if (syndrome(*cw))
  244. {
  245. *errs=1;
  246. return(2);
  247. }
  248. else
  249. return(0); /* no errors */
  250. }
  251. } /* decode */
  252.  
  253. /* ====================================================== */
  254.  
  255. void golay_test(void)
  256. /* This function tests the Golay routines for detection and correction
  257.   of various patterns of error_limit bit errors. The error_mask cycles
  258.   over all possible values, and error_limit selects the maximum number
  259.   of induced errors. */
  260. {
  261. unsigned long
  262. error_mask, /* bitwise mask for inducing errors */
  263. trashed_codeword, /* the codeword for trial correction */
  264. virgin_codeword; /* the original codeword without errors */
  265. unsigned char
  266. pass=1, /* assume test passes */
  267. error_limit=3; /* select number of induced bit errors here */
  268. int
  269. error_count; /* receives number of errors corrected */
  270.  
  271. virgin_codeword=golay(0x555); /* make a test codeword */
  272. if (parity(virgin_codeword))
  273. virgin_codeword^=0x800000l;
  274. for (error_mask=0; error_mask<0x800000l; error_mask++)
  275. {
  276. /* filter the mask for the selected number of bit errors */
  277. if (weight(error_mask) <= error_limit) /* you can make this faster! */
  278. {
  279. trashed_codeword=virgin_codeword ^ error_mask; /* induce bit errors */
  280.  
  281. decode(1,&error_count,&trashed_codeword); /* try to correct bit errors */
  282.  
  283. if (trashed_codeword ^ virgin_codeword)
  284. {
  285. printf("Unable to correct %d errors induced with error mask = 0x%lX\n",
  286. weight(error_mask),error_mask);
  287. pass=0;
  288. }
  289.  
  290. if (kbhit()) /* look for user input */
  291. {
  292. if (getch() == 27) return; /* escape exits */
  293.  
  294. /* other key prints status */
  295. printf("Current test count = %ld of %ld\n",error_mask,0x800000l);
  296. }
  297. }
  298. }
  299. printf("Golay test %s!\n",pass?"PASSED":"FAILED");
  300. }
  301.  
  302. /* ====================================================== */
  303.  
  304. void main(int argument_count, char *argument[])
  305. {
  306. int i,j;
  307. unsigned long l,g;
  308. const char *errmsg = "Usage: G DATA Encode/Correct/Verify/Test\n\n"
  309. " where DATA is the data to be encoded, codeword to be corrected,\n"
  310. " or codeword to be checked for errors. DATA is hexadecimal.\n\n"
  311. "Examples:\n\n"
  312. " G 555 E encodes information value 555 and prints a codeword\n"
  313. " G ABC123 C corrects codeword ABC123\n"
  314. " G ABC123 V checks codeword ABC123 for errors\n"
  315. " G ABC123 T tests routines, ABC123 is a dummy parameter\n\n";
  316.  
  317. if (argument_count != 3)
  318. {
  319. printf(errmsg);
  320. exit(0);
  321. }
  322.  
  323. if (sscanf(argument[1],"%lx",&l) != 1)
  324. {
  325. printf(errmsg);
  326. exit(0);
  327. }
  328.  
  329. switch (toupper(*argument[2]))
  330. {
  331. case 'E': /* encode */
  332. l&=0xfff;
  333. l=golay(l);
  334. if (parity(l)) l^=0x800000l;
  335. printf("Codeword = %lX\n",l);
  336. break;
  337.  
  338. case 'V': /* verify */
  339. if (decode(0,&i,&l))
  340. printf("Codeword %lX is not a Golay codeword.\n",l);
  341. else
  342. printf("Codeword %lX is a Golay codeword.\n",l);
  343. break;
  344.  
  345. case 'C': /* correct */
  346. g=l; /* save initial codeword */
  347. j=decode(1,&i,&l);
  348. if ((j) && (i))
  349. printf("Codeword %lX had %d bits corrected,\n"
  350. "resulting in codeword %lX with a parity error.\n",g,i,l);
  351. else
  352. if ((j == 0) && (i))
  353. printf("Codeword %lX had %d bits corrected, resulting in codeword %lX.\n",g,i,l);
  354. else
  355. if ((j) && (i == 0))
  356. printf("Codeword %lX has a parity error. No bits were corrected.\n",g);
  357. else
  358. if ((j == 0) && (i == 0))
  359. printf("Codeword %lX does not require correction.\n",g);
  360. break;
  361.  
  362. case 'T': /* test */
  363. printf("Press SPACE for status, ESC to exit test...\n");
  364. golay_test();
  365. break;
  366.  
  367. default:
  368. printf(errmsg);
  369. exit(0);
  370. }
  371. }
  372.  
  373. /* end of G.C */
  374.  
  375.  
  376. import java.util.*;
  377. import java.lang.*;
  378. import java.io.*;
  379.  
  380. /* Name of the class has to be "Main" only if the class is public. */
  381. class Ideone
  382. {
  383. public static void main (String[] args) throws java.lang.Exception
  384. {
  385. // your code goes here
  386. }
  387. }
Success #stdin #stdout 0.03s 25712KB
stdin
Standard input is empty
stdout
/* package whatever; // don't place package name! */
/*
    
    This source code accompanies the article, "Using The Golay Error Detection And
    Correction Code", by Hank Wallace. This program demonstrates use of the Golay
    code.
      Usage: G DATA Encode/Correct/Verify/Test
        where DATA is the data to be encoded, codeword to be corrected,
        or codeword to be checked for errors. DATA is hexadecimal.
      Examples:
        G 555 E      encodes information value 555 and prints a codeword
        G ABC123 C   corrects codeword ABC123
        G ABC123 V   checks codeword ABC123 for errors
        G ABC123 T   tests routines, ABC123 is a dummy parameter
    This program may be freely incorporated into your programs as needed. It
    compiles under Borland's Turbo C 2.0. No warranty of any kind is granted.
    */
    #include "stdio.h"
    #include "conio.h"
    #define POLY  0xAE3  /* or use the other polynomial, 0xC75 */
    
    /* ====================================================== */
    
    unsigned long golay(unsigned long cw)
    /* This function calculates [23,12] Golay codewords.
      The format of the returned longint is
      [checkbits(11),data(12)]. */
    {
      int i;
      unsigned long c;
      cw&=0xfffl;
      c=cw; /* save original codeword */
      for (i=1; i<=12; i++)  /* examine each data bit */
        {
          if (cw & 1)        /* test data bit */
            cw^=POLY;        /* XOR polynomial */
          cw>>=1;            /* shift intermediate result */
        }
      return((cw<<12)|c);    /* assemble codeword */
    }
    
    /* ====================================================== */
    
    int parity(unsigned long cw)
    /* This function checks the overall parity of codeword cw.
      If parity is even, 0 is returned, else 1. */
    {
      unsigned char p;
    
      /* XOR the bytes of the codeword */
      p=*(unsigned char*)&cw;
      p^=*((unsigned char*)&cw+1);
      p^=*((unsigned char*)&cw+2);
    
      /* XOR the halves of the intermediate result */
      p=p ^ (p>>4);
      p=p ^ (p>>2);
      p=p ^ (p>>1);
    
      /* return the parity result */
      return(p & 1);
    }
    
    /* ====================================================== */
    
    unsigned long syndrome(unsigned long cw)
    /* This function calculates and returns the syndrome
      of a [23,12] Golay codeword. */
    {
      int i;
      cw&=0x7fffffl;
      for (i=1; i<=12; i++)  /* examine each data bit */
        {
          if (cw & 1)        /* test data bit */
            cw^=POLY;        /* XOR polynomial */
          cw>>=1;            /* shift intermediate result */
        }
      return(cw<<12);        /* value pairs with upper bits of cw */
    }
    
    /* ====================================================== */
    
    int weight(unsigned long cw)
    /* This function calculates the weight of
      23 bit codeword cw. */
    {
      int bits,k;
    
      /* nibble weight table */
      const char wgt[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    
      bits=0; /* bit counter */
      k=0;
      /* do all bits, six nibbles max */
      while ((k<6) && (cw))
        {
          bits=bits+wgt[cw & 0xf];
          cw>>=4;
          k++;
        }
    
      return(bits);
    }
    
    /* ====================================================== */
    
    unsigned long rotate_left(unsigned long cw, int n)
    /* This function rotates 23 bit codeword cw left by n bits. */
    {
      int i;
    
      if (n != 0)
        {
          for (i=1; i<=n; i++)
            {
              if ((cw & 0x400000l) != 0)
                cw=(cw << 1) | 1;
              else
                cw<<=1;
            }
        }
    
      return(cw & 0x7fffffl);
    }
    
    /* ====================================================== */
    
    unsigned long rotate_right(unsigned long cw, int n)
    /* This function rotates 23 bit codeword cw right by n bits. */
    {
      int i;
    
      if (n != 0)
        {
          for (i=1; i<=n; i++)
            {
              if ((cw & 1) != 0)
                cw=(cw >> 1) | 0x400000l;
              else
                cw>>=1;
            }
        }
    
      return(cw & 0x7fffffl);
    }
    
    /* ====================================================== */
    
    unsigned long correct(unsigned long cw, int *errs)
    /* This function corrects Golay [23,12] codeword cw, returning the
      corrected codeword. This function will produce the corrected codeword
      for three or fewer errors. It will produce some other valid Golay
      codeword for four or more errors, possibly not the intended
      one. *errs is set to the number of bit errors corrected. */
    {
      unsigned char
        w;                /* current syndrome limit weight, 2 or 3 */
      unsigned long
        mask;             /* mask for bit flipping */
      int
        i,j;              /* index */
      unsigned long
        s,                /* calculated syndrome */
        cwsaver;          /* saves initial value of cw */
    
      cwsaver=cw;         /* save */
      *errs=0;
      w=3;                /* initial syndrome weight threshold */
      j=-1;               /* -1 = no trial bit flipping on first pass */
      mask=1;
      while (j<23) /* flip each trial bit */
        {
          if (j != -1) /* toggle a trial bit */
            {
              if (j>0) /* restore last trial bit */
                {
                  cw=cwsaver ^ mask;
                  mask+=mask; /* point to next bit */
                }
              cw=cwsaver ^ mask; /* flip next trial bit */
              w=2; /* lower the threshold while bit diddling */
            }
    
          s=syndrome(cw); /* look for errors */
          if (s) /* errors exist */
            {
              for (i=0; i<23; i++) /* check syndrome of each cyclic shift */
                {
                  if ((*errs=weight(s)) <= w) /* syndrome matches error pattern */
                    {
                      cw=cw ^ s;              /* remove errors */
                      cw=rotate_right(cw,i);  /* unrotate data */
                      return(s=cw);
                    }
                  else
                    {
                      cw=rotate_left(cw,1);   /* rotate to next pattern */
                      s=syndrome(cw);         /* calc new syndrome */
                    }
                }
              j++; /* toggle next trial bit */
            }
          else
            return(cw); /* return corrected codeword */
        }
    
      return(cwsaver); /* return original if no corrections */
    } /* correct */
    
    /* ====================================================== */
    
    int decode(int correct_mode, int *errs, unsigned long *cw)
    /* This function decodes codeword *cw in one of two modes. If correct_mode
      is nonzero, error correction is attempted, with *errs set to the number of
      bits corrected, and returning 0 if no errors exist, or 1 if parity errors
      exist. If correct_mode is zero, error detection is performed on *cw,
      returning 0 if no errors exist, 1 if an overall parity error exists, and
      2 if a codeword error exists. */
    {
      unsigned long parity_bit;
    
      if (correct_mode)               /* correct errors */
        {
          parity_bit=*cw & 0x800000l; /* save parity bit */
          *cw&=~0x800000l;            /* remove parity bit for correction */
    
          *cw=correct(*cw, errs);     /* correct up to three bits */
          *cw|=parity_bit;            /* restore parity bit */
    
          /* check for 4 bit errors */
          if (parity(*cw))            /* odd parity is an error */
            return(1);
          return(0); /* no errors */
        }
      else /* detect errors only */
        {
          *errs=0;
          if (parity(*cw)) /* odd parity is an error */
            {
              *errs=1;
              return(1);
            }
          if (syndrome(*cw))
            {
              *errs=1;
              return(2);
            }
          else
            return(0); /* no errors */
        }
    } /* decode */
    
    /* ====================================================== */
    
    void golay_test(void)
    /* This function tests the Golay routines for detection and correction
      of various patterns of error_limit bit errors. The error_mask cycles
      over all possible values, and error_limit selects the maximum number
      of induced errors. */
    {
      unsigned long
        error_mask,         /* bitwise mask for inducing errors */
        trashed_codeword,   /* the codeword for trial correction */
        virgin_codeword;    /* the original codeword without errors */
      unsigned char
        pass=1,             /* assume test passes */
        error_limit=3;      /* select number of induced bit errors here */
      int
        error_count;        /* receives number of errors corrected */
    
      virgin_codeword=golay(0x555); /* make a test codeword */
      if (parity(virgin_codeword))
        virgin_codeword^=0x800000l;
      for (error_mask=0; error_mask<0x800000l; error_mask++)
        {
          /* filter the mask for the selected number of bit errors */
          if (weight(error_mask) <= error_limit) /* you can make this faster! */
            {
              trashed_codeword=virgin_codeword ^ error_mask; /* induce bit errors */
    
              decode(1,&error_count,&trashed_codeword); /* try to correct bit errors */
    
              if (trashed_codeword ^ virgin_codeword)
                {
                  printf("Unable to correct %d errors induced with error mask = 0x%lX\n",
                    weight(error_mask),error_mask);
                  pass=0;
                }
    
              if (kbhit()) /* look for user input */
                {
                  if (getch() == 27) return; /* escape exits */
    
                  /* other key prints status */
                  printf("Current test count = %ld of %ld\n",error_mask,0x800000l);
                }
            }
        }
      printf("Golay test %s!\n",pass?"PASSED":"FAILED");
    }
    
    /* ====================================================== */
    
    void main(int argument_count, char *argument[])
    {
      int i,j;
      unsigned long l,g;
      const char *errmsg = "Usage: G DATA Encode/Correct/Verify/Test\n\n"
                 "  where DATA is the data to be encoded, codeword to be corrected,\n"
                 "  or codeword to be checked for errors. DATA is hexadecimal.\n\n"
                 "Examples:\n\n"
                 "  G 555 E      encodes information value 555 and prints a codeword\n"
                 "  G ABC123 C   corrects codeword ABC123\n"
                 "  G ABC123 V   checks codeword ABC123 for errors\n"
                 "  G ABC123 T   tests routines, ABC123 is a dummy parameter\n\n";
    
      if (argument_count != 3)
        {
          printf(errmsg);
          exit(0);
        }
    
      if (sscanf(argument[1],"%lx",&l) != 1)
        {
          printf(errmsg);
          exit(0);
        }
    
      switch (toupper(*argument[2]))
        {
          case 'E': /* encode */
            l&=0xfff;
            l=golay(l);
            if (parity(l)) l^=0x800000l;
            printf("Codeword = %lX\n",l);
            break;
    
          case 'V': /* verify */
            if (decode(0,&i,&l))
              printf("Codeword %lX is not a Golay codeword.\n",l);
            else
              printf("Codeword %lX is a Golay codeword.\n",l);
            break;
    
          case 'C': /* correct */
            g=l; /* save initial codeword */
            j=decode(1,&i,&l);
            if ((j) && (i))
              printf("Codeword %lX had %d bits corrected,\n"
                     "resulting in codeword %lX with a parity error.\n",g,i,l);
            else
              if ((j == 0) && (i))
                printf("Codeword %lX had %d bits corrected, resulting in codeword %lX.\n",g,i,l);
              else
                if ((j) && (i == 0))
                  printf("Codeword %lX has a parity error. No bits were corrected.\n",g);
                else
                  if ((j == 0) && (i == 0))
                    printf("Codeword %lX does not require correction.\n",g);
            break;
    
          case 'T': /* test */
            printf("Press SPACE for status, ESC to exit test...\n");
            golay_test();
            break;
    
          default:
            printf(errmsg);
            exit(0);
        }
    }
    
    /* end of G.C */


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
	}
}