Skip to content

Commit 763cf5b

Browse files
committed
Corrigidos métodos de codificação Hamming e criados testes unitários
1 parent 6cd7faa commit 763cf5b

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

src/main/java/core/util/BitUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public static int bitSetLength(int bitset) {
1818
}
1919

2020
/**
21-
* Define o valor de um bit especificado como 1
21+
* Muda o estado de um bit na posicao especificada
2222
*
2323
* @param data Valor original
24-
* @param index Index do bit a ser setado
24+
* @param index Index do bit a ser alterado
2525
* @return {@code int}
2626
*/
27-
public static int setBit(int data, int index) {
27+
public static int toggleBit(int data, int index) {
2828
if (index < 0 || index > (Integer.SIZE - 1)) throw new IllegalArgumentException();
29-
return data | (1 << index);
29+
return data ^ (1 << index);
3030
}
3131

3232
}

src/main/java/errorcorrecting/hamming/HammingUtils.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,31 @@ public static int parseAndCorrectCodeword(int codeword) {
5656
// 3 2 1 0 <= posicao
5757
switch (hammingCheck) {
5858
case 0b101: // r1, r4 = m1
59-
symbol = BitUtils.setBit(symbol, 3);
59+
symbol = BitUtils.toggleBit(symbol, 3);
60+
break;
6061
case 0b111: // r1, r2, r4 = m2
61-
symbol = BitUtils.setBit(symbol, 2);
62+
symbol = BitUtils.toggleBit(symbol, 2);
63+
break;
6264
case 0b110: // r1, r2 = m3
63-
symbol = BitUtils.setBit(symbol, 1);
65+
symbol = BitUtils.toggleBit(symbol, 1);
66+
break;
6467
case 0b011: // r2, r4 = m4
65-
symbol = BitUtils.setBit(symbol, 0);
68+
symbol = BitUtils.toggleBit(symbol, 0);
69+
break;
6670
}
6771
return symbol;
6872
}
6973

7074
private static int getFirstBitHamming(int b) {
71-
return (b ^ 0b1101) % 2 == 0 ? 0 : 1;
75+
return Integer.bitCount(b & 0b1110) % 2 == 0 ? 0 : 1;
7276
}
7377

7478
private static int getSecondBitHamming(int b) {
75-
return (b ^ 0b0111) % 2 == 0 ? 0 : 1;
79+
return Integer.bitCount(b & 0b0111) % 2 == 0 ? 0 : 1;
7680
}
7781

7882
private static int getThirdBitHamming(int b) {
79-
return (b ^ 0b1101) % 2 == 0 ? 0 : 1;
83+
return Integer.bitCount(b & 0b1101) % 2 == 0 ? 0 : 1;
8084
}
8185

8286
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package errorcorrectiong.hamming;
2+
3+
import errorcorrecting.hamming.HammingUtils;
4+
import org.junit.jupiter.api.Test;
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class HammingUtilsTest {
8+
9+
@Test
10+
void getCodewordTest() {
11+
assertEquals(0, HammingUtils.getCodeword(0));
12+
assertEquals(41, HammingUtils.getCodeword(1));
13+
assertEquals(75, HammingUtils.getCodeword(3));
14+
assertEquals(39, HammingUtils.getCodeword(7));
15+
assertEquals(127, HammingUtils.getCodeword(15));
16+
}
17+
18+
@Test
19+
void parseCodewordTest() {
20+
assertEquals(0, HammingUtils.parseCodeword(0));
21+
assertEquals(1, HammingUtils.parseCodeword(41));
22+
assertEquals(3, HammingUtils.parseCodeword(75));
23+
assertEquals(7, HammingUtils.parseCodeword(39));
24+
assertEquals(15, HammingUtils.parseCodeword(127));
25+
}
26+
27+
@Test
28+
void parseAndCorrectCodewordTest() {
29+
assertEquals(0, HammingUtils.parseAndCorrectCodeword(0));
30+
assertEquals(1, HammingUtils.parseAndCorrectCodeword(41));
31+
assertEquals(1, HammingUtils.parseAndCorrectCodeword(43));
32+
assertEquals(3, HammingUtils.parseAndCorrectCodeword(75));
33+
assertEquals(3, HammingUtils.parseAndCorrectCodeword(67));
34+
assertEquals(7, HammingUtils.parseAndCorrectCodeword(39));
35+
assertEquals(7, HammingUtils.parseAndCorrectCodeword(7));
36+
assertEquals(15, HammingUtils.parseAndCorrectCodeword(127));
37+
assertEquals(15, HammingUtils.parseAndCorrectCodeword(119));
38+
}
39+
40+
}

0 commit comments

Comments
 (0)