diff --git a/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java b/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java index 36c0790e565f..b3783ab284b2 100644 --- a/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java +++ b/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java @@ -30,4 +30,30 @@ public static long binaryToDecimal(long binaryNumber) { } return decimalValue; } + + /** + * Converts a binary String to its decimal equivalent using bitwise operators. + * + * @param binary The binary number to convert. + * @return The decimal equivalent of the binary number. + * @throws IllegalArgumentException If the binary number contains digits other than 0 and 1. + */ + public static long binaryStringToDecimal(String binary) { + boolean isNegative = binary.charAt(0) == '-'; + if (isNegative) { + binary = binary.substring(1); + } + + long decimalValue = 0; + + for (char bit : binary.toCharArray()) { + if (bit != '0' && bit != '1') { + throw new IllegalArgumentException("Incorrect binary digit: " + bit); + } + // shift left by 1 (multiply by 2) and add bit value + decimalValue = (decimalValue << 1) | (bit - '0'); + } + + return isNegative ? -decimalValue : decimalValue; + } } diff --git a/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java b/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java index 9045d100285e..e11a86b4c006 100644 --- a/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java +++ b/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java @@ -18,6 +18,12 @@ public void testBinaryToDecimal() { assertEquals(5, BinaryToDecimal.binaryToDecimal(101)); assertEquals(63, BinaryToDecimal.binaryToDecimal(111111)); assertEquals(512, BinaryToDecimal.binaryToDecimal(1000000000)); + + assertEquals(0, BinaryToDecimal.binaryStringToDecimal("0")); + assertEquals(1, BinaryToDecimal.binaryStringToDecimal("1")); + assertEquals(5, BinaryToDecimal.binaryStringToDecimal("101")); + assertEquals(63, BinaryToDecimal.binaryStringToDecimal("111111")); + assertEquals(512, BinaryToDecimal.binaryStringToDecimal("1000000000")); } @Test @@ -25,6 +31,9 @@ public void testBinaryToDecimal() { public void testNegativeBinaryToDecimal() { assertEquals(-1, BinaryToDecimal.binaryToDecimal(-1)); assertEquals(-42, BinaryToDecimal.binaryToDecimal(-101010)); + + assertEquals(-1, BinaryToDecimal.binaryStringToDecimal("-1")); + assertEquals(-42, BinaryToDecimal.binaryStringToDecimal("-101010")); } @Test @@ -32,11 +41,16 @@ public void testNegativeBinaryToDecimal() { public void testLargeBinaryToDecimal() { assertEquals(262144L, BinaryToDecimal.binaryToDecimal(1000000000000000000L)); assertEquals(524287L, BinaryToDecimal.binaryToDecimal(1111111111111111111L)); + + assertEquals(262144L, BinaryToDecimal.binaryStringToDecimal("1000000000000000000")); + assertEquals(524287L, BinaryToDecimal.binaryStringToDecimal("1111111111111111111")); } @ParameterizedTest @CsvSource({"2", "1234", "11112", "101021"}) void testNotCorrectBinaryInput(long binaryNumber) { assertThrows(IllegalArgumentException.class, () -> BinaryToDecimal.binaryToDecimal(binaryNumber)); + + assertThrows(IllegalArgumentException.class, () -> BinaryToDecimal.binaryStringToDecimal(Long.toString(binaryNumber))); } }