The following function maybe used to calculate the CRC of a given polynomial
* @param ydividend D(x) the binary representation of the polynomial whose CRC is to be calculated
* @param xdivisor the polynomial that base P(x)
* X^4 + X^2+x+1 will be represented as 10111
* Note :The Dividend shouldn't be padded to include the divisor length
* Algorithm in partial 'C'
*
* input String dividend, String Divisor
* Dividend left shift dividend by length of divisor - 1;
* pos = dividend.length
* while (pos > lengthof(divisor))
* {
* temp <- divdident[pos,lengthof(divisor)]
* temp <-xor temp with divisor
* temp <- lshift(temp,1)
* temp[lengthof(divisor -1)] = dividend[pos - lengthof( pos)-1]
* pos <-pos - 1
*
* }
The solution uses java bit String
//testvectors
String xdivisor= "110101";
String ydividend = "1010001101";
remainder will be 001110
* @param ydividend D(x) the binary representation of the polynomial whose CRC is to be calculated
* @param xdivisor the polynomial that base P(x)
* X^4 + X^2+x+1 will be represented as 10111
* Note :The Dividend shouldn't be padded to include the divisor length
* Algorithm in partial 'C'
*
* input String dividend, String Divisor
* Dividend left shift dividend by length of divisor - 1;
* pos = dividend.length
* while (pos > lengthof(divisor))
* {
* temp <- divdident[pos,lengthof(divisor)]
* temp <-xor temp with divisor
* temp <- lshift(temp,1)
* temp[lengthof(divisor -1)] = dividend[pos - lengthof( pos)-1]
* pos <-pos - 1
*
* }
The solution uses java bit String
//testvectors
String xdivisor= "110101";
String ydividend = "1010001101";
remainder will be 001110
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | public static String calculateCrc(String ydividend, String xdivisor) throws NumberFormatException { //testvectors // String xdivisor= "110101"; // String ydividend = "1010001101"; // remainder will be 001110 BitSet divisor = new BitSet(xdivisor.length()); BitSet dividend = new BitSet(ydividend.length()); for ( int m = 0 ;m<ydividend.length();m++) { if (ydividend.charAt(m) == '1' ) { dividend.set(ydividend.length()-m- 1 ); } else if (ydividend.charAt(m)== '0' ) { dividend.clear(ydividend.length()-m- 1 ); } else { throw new NumberFormatException( "Invalid Bit String" ); } } for ( int k= 0 ;k<xdivisor.length();k++) { if (xdivisor.charAt(k) == '1' ) { divisor.set(xdivisor.length()-k- 1 ); } else if (xdivisor.charAt(k)== '0' ) { divisor.clear(xdivisor.length()-k- 1 ); } else { throw new NumberFormatException( "Invalid Bit String" ); } } BitSet temp = new BitSet(); int divisorlength = xdivisor.length(); int dividendlength = ydividend.length(); int dividendwid0len = divisorlength + dividendlength - 1 ; BitSet LeftShiftedDividend = new BitSet(dividendwid0len); for ( int g= 0 ;g<dividend.length();g++) { if (dividend.get(g) == true ) { LeftShiftedDividend.set(g); } } LeftShiftedDividend =leftshift(LeftShiftedDividend,divisorlength- 1 ); temp = LeftShiftedDividend.get(LeftShiftedDividend.length()-divisorlength,LeftShiftedDividend.length()); BitSet tempDivisor = divisor.get( 0 ,divisor.length()); for ( int z= 0 ;z<dividendwid0len-divisorlength;z++) { temp.xor(tempDivisor); temp = leftshift(temp, 1 ); temp.set( 0 ,LeftShiftedDividend.get(dividendwid0len-divisorlength-z- 1 )); if (temp.length()> 0 ){ if (temp.get(divisor.length()- 1 )== false ) { tempDivisor = new BitSet(temp.length()); } else { tempDivisor = divisor; }} else { tempDivisor = new BitSet(temp.length()); } } if (temp.length()> 0 ){ if (temp.get(divisor.length()- 1 )== true ) { temp.xor(divisor); } } String theOutputCRC= "" ; for ( int r= 0 ;r<divisorlength;r++) { if (temp.get(r)== false ) { theOutputCRC= "0" +theOutputCRC; } else { theOutputCRC= "1" +theOutputCRC; } } return (theOutputCRC); } |