-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcn_lab3.java
More file actions
138 lines (117 loc) · 2.96 KB
/
dcn_lab3.java
File metadata and controls
138 lines (117 loc) · 2.96 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
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
package dcn_lab1;
//Java code to implement the approach
import java.util.Arrays;
class dcn_lab3 {
//Returns XOR of 'a' and 'b'
//(both of same length)
static String Xor(String a, String b)
{
// Initialize result
String result = "";
int n = b.length();
// Traverse all bits, if bits are
// same, then XOR is 0, else 1
for (int i = 1; i < n; i++) {
if (a.charAt(i) == b.charAt(i))
result += "0";
else
result += "1";
}
return result;
}
//Performs Modulo-2 division
static String Mod2Div(String dividend, String divisor)
{
// Number of bits to be XORed at a time.
int pick = divisor.length();
// Slicing the dividend to appropriate
// length for particular step
String tmp = dividend.substring(0, pick);
int n = dividend.length();
while (pick < n) {
if (tmp.charAt(0) == '1')
// Replace the dividend by the result
// of XOR and pull 1 bit down
tmp = Xor(divisor, tmp)
+ dividend.charAt(pick);
else
// If leftmost bit is '0'.
// If the leftmost bit of the dividend (or
// the part used in each step) is 0, the
// step cannot use the regular divisor; we
// need to use an all-0s divisor.
tmp = Xor(new String(new char[pick])
.replace("\0", "0"),
tmp)
+ dividend.charAt(pick);
// Increment pick to move further
pick += 1;
}
// For the last n bits, we have to carry it out
// normally as increased value of pick will cause
// Index Out of Bounds.
if (tmp.charAt(0) == '1')
tmp = Xor(divisor, tmp);
else
tmp = Xor(new String(new char[pick])
.replace("\0", "0"),
tmp);
return tmp;
}
//Function used at the sender side to encode
//data by appending remainder of modular division
//at the end of data.
static void EncodeData(String data, String key)
{
int l_key = key.length();
// Appends n-1 zeroes at end of data
String appended_data
= (data
+ new String(new char[l_key - 1])
.replace("\0", "0"));
String remainder = Mod2Div(appended_data, key);
// Append remainder in the original data
String codeword = data + remainder;
System.out.println("Remainder : " + remainder
+ "\n");
System.out.println(
"Encoded Data (Data + Remainder) :" + codeword
+ "\n");
}
//checking if the message received by receiver is
//correct or not. If the remainder is all 0 then it is
//correct, else wrong.
static void Receiver(String data, String key)
{
String currxor
= Mod2Div(data.substring(0, key.length()), key);
int curr = key.length();
while (curr != data.length()) {
if (currxor.length() != key.length()) {
currxor += data.charAt(curr++);
}
else {
currxor = Mod2Div(currxor, key);
}
}
if (currxor.length() == key.length()) {
currxor = Mod2Div(currxor, key);
}
if (currxor.contains("1")) {
System.out.println(
"there is some error in data");
}
else {
System.out.println("correct message received");
}
}
//Driver code
public static void main(String[] args)
{
String data = "100100";
String key = "1101";
EncodeData(data, key);
Receiver(data + Mod2Div(data, key), key);
}
}
//This code is contributed by phasing17