skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, October 4, 2012

CRC CALCULATION

Posted by Admin at 10:17 PM – 0 comments
 
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
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);
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger