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