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