forked from anmolkaur18/TestSample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCN_CRC_Error_detection.cpp
More file actions
102 lines (91 loc) · 2.21 KB
/
CN_CRC_Error_detection.cpp
File metadata and controls
102 lines (91 loc) · 2.21 KB
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
#include<iostream>
#include<string>
#include<conio.h>
#include<fstream>
using namespace std;
int main()
{
string msg;
int flag,i,j;
do
{
flag=0;
cout<<"\n\n Enter the message (having bits 0 or 1 only) : ";
cin>>msg;
for(i=0;i<msg.length();i++)
{
if(msg[i]!='0'&&msg[i]!='1')
flag=1;
}
}while(flag==1);
string divisor("1011");
string divisor_0("0000");
const int div_len=4;
string codeword(msg);
string remainder;
string temp_remainder;
for(i=0 ; i<div_len-1 ; i++)
{
codeword +='0';
}
cout<<"\n\n Codeword after appending "<<div_len-1<<" zeroes is : "<<codeword;
//for first division.
if(codeword[0]=='0') //use divisor_0.
{
for(i=1;i<div_len;i++)
{
if(codeword[i]=='0')
remainder+='0';
else
remainder+='1';
}
}
else
{
for(i=1;i<div_len;i++)
{
if(codeword[i]==divisor[i])
remainder+='0';
else
remainder+='1';
}
}
//for further divisions.
for(i ; i<codeword.length() ; i++)
{
remainder+=codeword[i];
if(remainder[0]=='0') //use divisor_0.
{
for(j=1;j<div_len;j++)
{
if(remainder[j]=='0')
temp_remainder +='0';
else
temp_remainder += '1';
}
}
else //use divisor.
{
for(j=1;j<div_len;j++)
{
if(remainder[j]==divisor[j])
temp_remainder +='0';
else
temp_remainder += '1';
}
}
remainder=temp_remainder;
temp_remainder.clear();
}
cout<<"\n\n Remainder of the division performed is : "<<remainder;
codeword.replace(msg.length() , div_len-1 , remainder);
cout<<"\n\n Codeword is : "<<codeword;
cout<<"\n\n Dataword is : "<<msg;
cout<<"\n\n Remainder is : "<<remainder<<"\n\n";
ofstream fout;
fout.open("crc.txt",ios::out);
fout<<codeword;
fout.close();
getch();
return 0;
}