-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrc.cpp
138 lines (128 loc) · 2.44 KB
/
crc.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include<iostream>
#include<vector>
#include<queue>
using namespace std ;
vector<int> input ;
int n,m ;
int temp ;
vector<int> poly ;
queue<int> remainder ;
void printq(queue<int> q){
while(!q.empty())
{
cout<<q.front()<<" " ;
q.pop() ;
}
cout<<endl ;
}
void clearq(queue<int> &q)
{
while(q.size())
q.pop() ;
}
queue<int> get_remainder()
{
int index=0 ;
queue<int> remainder,div ;
//move to first 1
while(input[index]!=1 && index<m)
index++ ;
//initialize div
for(int i=0 ; i<n ; i++)
{
div.push(input[index]) ;
index++ ;
}
//perform division
while(index<m)
{
clearq(remainder) ;
for(int i=0 ; i<n ; i++)
{
remainder.push(div.front()^poly[i]) ;
div.pop() ;
}
//move to first 1 and fill div
div = remainder ;
while(!div.empty() && div.front()==0)
div.pop() ;
while(div.size()<n && index<m)
{
div.push(input[index]) ;
index++ ;
if(div.front()==0)
div.pop() ;
}
}
//handles last division
if(div.size()==n)
{
clearq(remainder) ;
for(int i=0 ; i<n ; i++)
{
remainder.push(div.front()^poly[i]);
div.pop() ;
}
}
remainder.pop() ;
return remainder ;
}
int main()
{
cout<<"Enter degree of polynomial : " ;
cin>>n ;
cout<<"\nEnter polynomial bits :\n" ;
for(int i=0 ; i<n ; i++)
{
cin>>temp ;
poly.push_back(temp) ;
}
cout<<"\nEnter input length : " ;
cin>>m ;
cout<<"\nEnter input bit sequence :\n" ;
for(int i=0 ; i<m ; i++){
cin>>temp ;
input.push_back(temp) ;
}
m = m+n-1 ;
//push (n-1) zeros
for(int i=1 ; i<n ; i++)
input.push_back(0) ;
//......................................//
remainder = get_remainder() ;
printq(remainder) ;
int j=remainder.size() ;
while(!remainder.empty())
{
input[m-j] = remainder.front() ;
remainder.pop() ;
j-- ;
}
cout<<"Modified Sequence : " ;
for(int i=0 ; i<m ; i++)
{
cout<<input[i]<<" " ;
}
cout<<endl ;
//......................................//
int bitno ;
cout<<"Enter bit number to flip (Enter -1 to make no change) : " ;
cin>>bitno ;
if(bitno>-1)
input[bitno] = 1-input[bitno] ;
//......................................//
cout<<"Answer : \n" ;
remainder = get_remainder() ;
while(!remainder.empty())
{
if(remainder.front())
{
cout<<"Error Detected!\n" ;
break ;
}
remainder.pop() ;
if(remainder.size()==0)
cout<<"No error!\n" ;
}
return 0 ;
}