1
+ package dcn_lab1 ;
2
+ import java .util .*;
3
+ public class HammingCode {
4
+ public static void main (String args [])
5
+ {
6
+ int size , hammingCodeSize , errorPosition ,arr [],hammingCode [];
7
+ Scanner sc = new Scanner (System .in );
8
+ System .out .println ("enter data size (in bits)=" );
9
+ size = sc .nextInt ();
10
+ arr = new int [size ];
11
+ for (int j = 0 ; j < size ; j ++) { // INPUT DATA
12
+ System .out .println ("Enter " + (size - j ) + "-bit of the data=" );
13
+ arr [size - j - 1 ] = sc .nextInt ();
14
+ }
15
+ hammingCode = getHammingCode (arr ); // store return value to the hammingCode array
16
+ hammingCodeSize = hammingCode .length ;
17
+ System .out .println ("the entered data is=" );
18
+ for (int i = 0 ; i < hammingCodeSize ; i ++) {
19
+ System .out .print (hammingCode [(hammingCodeSize - i - 1 )]);
20
+ }
21
+ System .out .println ("enter the position you want to generate error in(if not 0)= " );
22
+ errorPosition = sc .nextInt ();
23
+ sc .close ();
24
+ if (errorPosition != 0 ) {
25
+ // alter bit of the user entered position
26
+ hammingCode [errorPosition - 1 ] = (hammingCode [errorPosition - 1 ] + 1 ) % 2 ;
27
+ }
28
+ System .out .println ("the sender data= " );
29
+ for (int k = 0 ; k < hammingCodeSize ; k ++) {
30
+ System .out .print (hammingCode [hammingCodeSize - k - 1 ]);
31
+ }
32
+ System .out .println (); // for next line
33
+ receiveData (hammingCode , hammingCodeSize - arr .length );
34
+ }
35
+ static int [] getHammingCode (int data []) {
36
+ int returnData [],size ,i = 0 , parityBits = 0 ,j = 0 , k = 0 ;
37
+ size = data .length ;
38
+ while (i < size ) {
39
+ // 2 power of parity bits = current position(number of bits traversed + number of parity bits + 1).
40
+ if (Math .pow (2 , parityBits ) == (i + parityBits + 1 )) {
41
+ parityBits ++;
42
+ }
43
+ else {
44
+ i ++;
45
+ }
46
+ }
47
+ returnData = new int [size + parityBits ];
48
+ // initialize returnData array with '2'
49
+ for (i = 1 ; i <= returnData .length ; i ++) { // condition to find parity bit location
50
+ if (Math .pow (2 , j ) == i ) {
51
+
52
+ returnData [(i - 1 )] = 2 ;
53
+ j ++;
54
+ }
55
+ else {
56
+ returnData [(k + j )] = data [k ++]; //for odd position
57
+ }
58
+ }
59
+ for (i = 0 ; i < parityBits ; i ++) {// use for loop to set even parity bits at parity bit locations
60
+
61
+ returnData [((int ) Math .pow (2 , i )) - 1 ] = getParityBit (returnData , i );
62
+ }
63
+
64
+ return returnData ;
65
+ }
66
+ static int getParityBit (int returnData [], int pow ) { //return parity bit based on the power
67
+ int parityBit = 0 ;
68
+ int size = returnData .length ;
69
+ for (int i = 0 ; i < size ; i ++) {
70
+ if (returnData [i ] != 2 ) {
71
+ int k = (i + 1 );
72
+ String str = Integer .toBinaryString (k );
73
+ //Now bit at the 2^(power) location of the binary value of index is 1,
74
+ // we check the value stored at that location. If the value is 1 or 0,
75
+ // we will calculate the parity value.
76
+ int temp = ((Integer .parseInt (str )) / ((int ) Math .pow (10 , pow ))) % 10 ;
77
+ if (temp == 1 ) {
78
+ if (returnData [i ] == 1 ) {
79
+ parityBit = (parityBit + 1 ) % 2 ;
80
+ }
81
+ }
82
+ }
83
+ }
84
+ return parityBit ;
85
+ }
86
+ static void receiveData (int data [], int parityBits ) { //Detect error
87
+ int pow ;
88
+ int size = data .length ;
89
+ int parityArray [] = new int [parityBits ];
90
+ String errorLoc = new String (); // for storing the integer value of the error location
91
+ for (pow = 0 ; pow < parityBits ; pow ++) {
92
+ for (int i = 0 ; i < size ; i ++) { //used for 2^power position
93
+ int j = i + 1 ;
94
+ String str = Integer .toBinaryString (j );
95
+ int bit = ((Integer .parseInt (str )) / ((int ) Math .pow (10 , pow ))) % 10 ; //find bit
96
+ if (bit == 1 ) {
97
+ if (data [i ] == 1 ) {
98
+ parityArray [pow ] = (parityArray [pow ] + 1 ) % 2 ;
99
+ }
100
+ }
101
+ }
102
+ errorLoc = parityArray [pow ] + errorLoc ;
103
+ }
104
+ int finalLoc = Integer .parseInt (errorLoc , 2 );
105
+ // check whether the finalLoc value is 0 or not
106
+ if (finalLoc != 0 ) {
107
+ System .out .println ("error at location= " + finalLoc );
108
+ data [finalLoc - 1 ] = (data [finalLoc - 1 ] + 1 ) % 2 ;
109
+ System .out .println ("After correcting the error, the code is:" );
110
+ for (int i = 0 ; i < size ; i ++) {
111
+ System .out .print (data [size - i - 1 ]);
112
+ }
113
+ }
114
+ else {
115
+ System .out .println ("no error!!" );
116
+ }
117
+ }
118
+ }
0 commit comments