diff --git a/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java b/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java new file mode 100644 index 000000000..c5c068422 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java @@ -0,0 +1,28 @@ +package com.thealgorithms.bitmanipulation; + +/** + * @author - https://github.com/Monk-AbhinayVerma + * @Wikipedia - https://en.wikipedia.org/wiki/Ones%27_complement + * The class OnesComplement computes the complement of binary number + * and returns + * the complemented binary string. + * @return the complimented binary string + */ +public final class OnesComplement { + private OnesComplement() { + } + + // Function to get the 1's complement of a binary number + public static String onesComplement(String binary) { + StringBuilder complement = new StringBuilder(); + // Invert each bit to get the 1's complement + for (int i = 0; i < binary.length(); i++) { + if (binary.charAt(i) == '0') { + complement.append('1'); + } else { + complement.append('0'); + } + } + return complement.toString(); + } +} diff --git a/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java b/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java new file mode 100644 index 000000000..0bc200722 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java @@ -0,0 +1,41 @@ +package com.thealgorithms.bitmanipulation; + +/** + * @wikipedia - https://en.wikipedia.org/wiki/Two%27s_complement + * This Algorithm was first suggested by Jon Von Neumann + * @author - https://github.com/Monk-AbhinayVerma + * @return the two's complement of any binary number + */ +public final class TwosComplement { + private TwosComplement() { + } + + // Function to get the 2's complement of a binary number + public static String twosComplement(String binary) { + StringBuilder onesComplement = new StringBuilder(); + // Step 1: Find the 1's complement (invert the bits) + for (int i = 0; i < binary.length(); i++) { + if (binary.charAt(i) == '0') { + onesComplement.append('1'); + } else { + onesComplement.append('0'); + } + } + // Step 2: Add 1 to the 1's complement + StringBuilder twosComplement = new StringBuilder(onesComplement); + boolean carry = true; + for (int i = onesComplement.length() - 1; i >= 0; i--) { + if (onesComplement.charAt(i) == '1' && carry) { + twosComplement.setCharAt(i, '0'); + } else if (onesComplement.charAt(i) == '0' && carry) { + twosComplement.setCharAt(i, '1'); + carry = false; + } + } + // If there is still a carry, append '1' at the beginning + if (carry) { + twosComplement.insert(0, '1'); + } + return twosComplement.toString(); + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java b/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java new file mode 100644 index 000000000..6be4eb595 --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java @@ -0,0 +1,47 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * Test case for Highest Set Bit + * @author Abhinay Verma(https://github.com/Monk-AbhinayVerma) + */ +public class OnesComplementTest { + + @Test + public void testOnesComplementAllZeroes() { + + // Test cases with all-zero binary strings + assertEquals("1111", OnesComplement.onesComplement("0000")); + assertEquals("111", OnesComplement.onesComplement("000")); + assertEquals("11", OnesComplement.onesComplement("00")); + assertEquals("1", OnesComplement.onesComplement("0")); + } + + @Test + public void testOnesComplementAllOnes() { + // Test cases with all-one binary strings + assertEquals("0000", OnesComplement.onesComplement("1111")); + assertEquals("000", OnesComplement.onesComplement("111")); + assertEquals("00", OnesComplement.onesComplement("11")); + assertEquals("0", OnesComplement.onesComplement("1")); + } + + @Test + public void testOnesComplementMixedBits() { + // Test more mixed binary patterns + assertEquals("1010", OnesComplement.onesComplement("0101")); + assertEquals("0101", OnesComplement.onesComplement("1010")); + assertEquals("1100", OnesComplement.onesComplement("0011")); + assertEquals("0011", OnesComplement.onesComplement("1100")); + assertEquals("1001", OnesComplement.onesComplement("0110")); + } + + @Test + public void testOnesComplementEmptyString() { + // Test empty string scenario + assertEquals("", OnesComplement.onesComplement("")); + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java b/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java new file mode 100644 index 000000000..512e94b63 --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java @@ -0,0 +1,54 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * Test case for Highest Set Bit + * @author Abhinay Verma(https://github.com/Monk-AbhinayVerma) + */ +public class TwosComplementTest { + + @Test + public void testTwosComplementAllZeroes() { + // Test with a binary number consisting entirely of zeroes + assertEquals("10000", TwosComplement.twosComplement("0000")); + assertEquals("1000", TwosComplement.twosComplement("000")); + assertEquals("100", TwosComplement.twosComplement("00")); + assertEquals("10", TwosComplement.twosComplement("0")); + } + + @Test + public void testTwosComplementAllOnes() { + // Test with a binary number consisting entirely of ones + assertEquals("00001", TwosComplement.twosComplement("11111")); + assertEquals("0001", TwosComplement.twosComplement("1111")); + assertEquals("001", TwosComplement.twosComplement("111")); + assertEquals("01", TwosComplement.twosComplement("11")); + } + + @Test + public void testTwosComplementMixedBits() { + // Test with binary numbers with mixed bits + assertEquals("1111", TwosComplement.twosComplement("0001")); // 1's complement: 1110, then add 1: 1111 + assertEquals("1001", TwosComplement.twosComplement("0111")); // 1's complement: 1000 + assertEquals("11001", TwosComplement.twosComplement("00111")); // 1's complement: 11000, add 1: 11001 + assertEquals("011", TwosComplement.twosComplement("101")); // 1's complement: 010, add 1: 011 + } + + @Test + public void testTwosComplementSingleBit() { + // Test with single bit + assertEquals("10", TwosComplement.twosComplement("0")); + assertEquals("1", TwosComplement.twosComplement("1")); + } + + @Test + public void testTwosComplementWithLeadingZeroes() { + // Test with leading zeroes in the input + assertEquals("1111", TwosComplement.twosComplement("0001")); + assertEquals("101", TwosComplement.twosComplement("011")); + assertEquals("110", TwosComplement.twosComplement("010")); + } +}