Advertisement
hgr24

Untitled

Mar 29th, 2022
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. package Crc.java;
  2. import java.io.*;
  3. class Crc
  4. {
  5. public static void main(String args[]) throws IOException
  6. {
  7. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  8. int[ ] data;
  9. int[ ]div;
  10. int[ ]divisor;
  11. int[ ]rem;
  12. int[ ] crc;
  13. int data_bits, divisor_bits, tot_length;
  14. System.out.println("Enter number of data bits : ");
  15. data_bits=Integer.parseInt(br.readLine());
  16. data=new int[data_bits];
  17. System.out.println("Enter data bits : ");
  18. for(int i=0; i<data_bits; i++)
  19. data[i]=Integer.parseInt(br.readLine());
  20. System.out.println("Enter number of bits in divisor : ");
  21. divisor_bits=Integer.parseInt(br.readLine());
  22. divisor=new int[divisor_bits];
  23. System.out.println("Enter Divisor bits : ");
  24. for(int i=0; i<divisor_bits; i++)
  25. divisor[i]=Integer.parseInt(br.readLine());
  26. /* System.out.print("Data bits are : ");
  27. for(int i=0; i< data_bits; i++)
  28. System.out.print(data[i]);
  29. System.out.println();
  30. System.out.print("divisor bits are : ");
  31. for(int i=0; i< divisor_bits; i++)
  32. System.out.print(divisor[i]);
  33. System.out.println();
  34. */ tot_length=data_bits+divisor_bits-1;
  35. div=new int[tot_length];
  36. rem=new int[tot_length];
  37. crc=new int[tot_length];
  38. /*------------------ CRC GENERATION-----------------------*/
  39. for(int i=0;i<data.length;i++)
  40. div[i]=data[i];
  41. System.out.print("Dividend (after appending 0's) are : ");
  42. for(int i=0; i< div.length; i++)
  43. System.out.print(div[i]);
  44. System.out.println();
  45. for(int j=0; j<div.length; j++){
  46. rem[j] = div[j];
  47. }
  48. rem=divide(div, divisor, rem);
  49. for(int i=0;i<div.length;i++) //append dividend and ramainder
  50. {
  51. crc[i]=(div[i]^rem[i]);
  52. }
  53. System.out.println();
  54. System.out.println("CRC code : ");
  55. for(int i=0;i<crc.length;i++)
  56. System.out.print(crc[i]);
  57. /*-------------------ERROR DETECTION---------------------*/
  58. System.out.println();
  59. System.out.println("Enter CRC code of "+tot_length+" bits : ");
  60. for(int i=0; i<crc.length; i++)
  61. crc[i]=Integer.parseInt(br.readLine());
  62. /* System.out.print("crc bits are : ");
  63. for(int i=0; i< crc.length; i++)
  64. System.out.print(crc[i]);
  65. System.out.println();
  66. */
  67. for(int j=0; j<crc.length; j++){
  68.     rem[j] = crc[j];
  69.     }
  70.     rem=divide(crc, divisor, rem);
  71.     for(int i=0; i< rem.length; i++)
  72.     {
  73.     if(rem[i]!=0)
  74.     {
  75.     System.out.println("Error");
  76.     break;
  77.     }
  78.     if(i==rem.length-1)
  79.     System.out.println("No Error");
  80.     }
  81.     System.out.println("THANK YOU.... :)");
  82.     }
  83.     static int[] divide(int div[],int divisor[], int rem[])
  84.     {
  85.     int cur=0;
  86.     while(true)
  87.     {
  88.     for(int i=0;i<divisor.length;i++)
  89.     rem[cur+i]=(rem[cur+i]^divisor[i]);
  90.     while(rem[cur]==0 && cur!=rem.length-1)
  91.     cur++;
  92.     if((rem.length-cur)<divisor.length)
  93.     break;
  94.     }
  95.     return rem;
  96.     }
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement