-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitStuffing.java
More file actions
51 lines (44 loc) · 1.28 KB
/
BitStuffing.java
File metadata and controls
51 lines (44 loc) · 1.28 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
public class BitStuffing {
public static String bitStuff(String data) {
String result = "";
int count = 0;
for(int i = 0; i < data.length(); i++) {
char bit = data.charAt(i);
result += bit;
if(bit == '1') count++;
else count = 0;
if(count == 5) {
result += '0';
count = 0;
}
}
return result;
}
public static String bitDeStuff(String data) {
String result = "";
int count = 0;
for(int i = 0; i < data.length(); i++) {
char bit = data.charAt(i);
if(bit == '1') {
count++;
result += bit;
} else {
if(count == 5)
count = 0;
else {
result += bit;
count = 0;
}
}
}
return result;
}
public static void main(String[] args) {
String data = "1111101111110";
System.out.println("Original: " + data);
String stuffed = bitStuff(data);
System.out.println("Stuffed: " + stuffed);
String destuffed = bitDeStuff(stuffed);
System.out.println("Destuffed: " + destuffed);
}
}