Main Page | Class List | File List | Class Members | File Members

crc4.c

Go to the documentation of this file.
00001 /*
00002  * crc4.c 
00003  * 
00004  * functions for 4-bit CRC calculations
00005  * assumes no more than 12 info. bits
00006  * Max Luttrell 8/19/96
00007  */
00008 
00009 #include "standard.h"
00010 #include "bits.h"
00011 
00012 #define GENPOLY 0x0017 /* x^4 + x^2 + x + 1 */
00013 
00014 unsigned int makecrc4(unsigned int b)
00015 /*
00016  * Takes b as input, which should be the information vector
00017  * already multiplied by x^4 (ie. shifted over 4 bits), and
00018  * returns the crc for this input based on the defined generator
00019  * polynomial GENPOLY
00020  */
00021 {
00022   int i;
00023         
00024   i=1;
00025   while (b>=16) { /* >= 2^4, so degree(b) >= degree(genpoly) */
00026     if (getbitint(b,i) == 1)
00027       b ^= GENPOLY << (12-i); /* reduce with GENPOLY */
00028     i++;
00029   }
00030   return b;
00031 }
00032 
00033 int checkcrc4(unsigned int b)
00034 /*
00035  * Takes b as input, and determines if it passes CRC check.
00036  * It does this by finding out if it is a multiple of GENPOLY.
00037  * Returns 1 if passes, 0 if fails.
00038  */
00039 {
00040   int i;
00041         
00042   i=1;
00043   while (b>=16) { /* >= than 2^4, so degree(b) >= degree(genpoly) */
00044   if (getbitint(b,i) == 1)
00045     b ^= GENPOLY << (12-i); /* reduce with GENPOLY */
00046   i++;
00047   }
00048 /*  printf("b: %d\n",b); */
00049   return (b==0);
00050 }
00051 
00052 #if(0)
00053 main()
00054 {
00055         unsigned int data;
00056         byte crc;
00057 
00058         data = 0x06c0;
00059         crc = makecrc4(data);
00060         printf("crc : %x, data : %x\n",crc,data);
00061         data=data | crc;
00062         printf("data after OR : %x\n",data);
00063         /* data = data | 0x0080; */
00064         data = 0x06c7;
00065         printf("data after corruption : %x\n",data);
00066 
00067         printf("the crc check...%d\n",checkcrc4(data));
00068 }
00069 #endif

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1