Repository: TheAlgorithms/Java
Pull request #3270 (2022-09-15 16:13:19)

Added the algorithm of hamming distance in others inside newly created cn package
Hamming distance an algorithm used in **Computer Networking** which finds total no.of error bits between the sender data bits and the receiver data bits.

MihSawant: @siriak, can you please review the code uploaded

Patch:
diff --git a/src/main/java/com/thealgorithms/others/cn/HammingDistance.java b/src/main/java/com/thealgorithms/others/cn/HammingDistance.java
new file mode 100644
index 00000000..25367a97
--- /dev/null
+++ b/src/main/java/com/thealgorithms/others/cn/HammingDistance.java
@@ -0,0 +1,38 @@
+package com.thealgorithms.others.cn;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class HammingDistance {
+
+
+
+    public int getHammingDistanceBetweenBits(String senderBits, String receiverBits) {
+
+        if(senderBits.length() != receiverBits.length()) {
+            throw new IllegalArgumentException("Sender and Receiver bits should be same");
+        }
+
+        List<byte[]> byteArray = new ArrayList<>();
+
+        byteArray.add(senderBits.getBytes());
+        byteArray.add(receiverBits.getBytes());
+
+
+        byte[] senderData = byteArray.get(0);
+        byte[] receiverData = byteArray.get(1);
+
+        int totalErrorBitCount = 0;
+
+        for(int i = 0; i < senderData.length; i++){
+           totalErrorBitCount += senderData[i] ^ receiverData[i];
+        }
+
+        if(totalErrorBitCount == 0){
+            System.out.println("No Error bit in data segments");
+        } else{
+            System.out.println("Total Error bit count "+totalErrorBitCount);
+        }
+        return totalErrorBitCount;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/cn/HammingDistanceTest.java b/src/test/java/com/thealgorithms/others/cn/HammingDistanceTest.java
new file mode 100644
index 00000000..66716af8
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/cn/HammingDistanceTest.java
@@ -0,0 +1,76 @@
+package com.thealgorithms.others.cn;
+
+import org.assertj.core.api.Assertions;
+import org.assertj.core.api.ThrowableTypeAssert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+
+public class HammingDistanceTest {
+
+    HammingDistance hd;
+
+    @BeforeEach
+     void initialize(){
+        hd = new HammingDistance();
+    }
+
+    @Test
+    public void checkForDifferentBits(){
+         String senderBits = "000", receiverBits = "011";
+        int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
+        Assertions.assertThat(answer).isEqualTo(2);
+    }
+/*
+
+    1 0 1 0 1
+    1 1 1 1 0
+    ----------
+    0 1 0 1 1
+
+
+ */
+    @Test
+    public void checkForDifferentBitsLength(){
+       String senderBits = "10101", receiverBits = "11110";
+        int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
+        Assertions.assertThat(answer).isEqualTo(3);
+    }
+
+
+    @Test
+    public void checkForSameBits(){
+        String senderBits = "111", receiverBits = "111";
+        int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
+        Assertions.assertThat(answer).isEqualTo(0);
+    }
+
+    @Test
+    public void checkForLongDataBits(){
+        String senderBits = "10010101101010000100110100", receiverBits = "00110100001011001100110101";
+        int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
+        Assertions.assertThat(answer).isEqualTo(7);
+    }
+
+    @Test
+    public void mismatchDataBits(){
+        String senderBits = "100010", receiverBits = "00011";
+
+        Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () ->{
+            int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
+        });
+
+        Assertions.assertThat(ex.getMessage()).contains("bits should be same");
+
+    }
+
+    @Test
+    public void checkForLongDataBitsSame(){
+        String senderBits = "10010101101010000100110100", receiverBits = "10010101101010000100110100";
+        int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
+        Assertions.assertThat(answer).isEqualTo(0);
+    }
+
+
+
+}