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