Repository: TheAlgorithms/Java
Pull request #4135 (2023-03-31 20:54:16)

Refactored BinaryToDecimal class 
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java b/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java
index de06ca6b..b0ab817d 100644
--- a/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java
+++ b/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java
@@ -7,12 +7,12 @@ import java.util.Scanner;
  */
 class BinaryToDecimal {
 
-    public static int binaryToDecimal(int binNum) {
-        int binCopy, d, s = 0, power = 0;
+    public static long binaryToDecimal(long binNum) {
+        long binCopy, d, s = 0, power = 0;
         binCopy = binNum;
         while (binCopy != 0) {
             d = binCopy % 10;
-            s += d * (int) Math.pow(2, power++);
+            s += d * (long) Math.pow(2, power++);
             binCopy /= 10;
         }
         return s;
@@ -26,7 +26,7 @@ class BinaryToDecimal {
     public static void main(String args[]) {
         Scanner sc = new Scanner(System.in);
         System.out.print("Binary number: ");
-        System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextInt()));
+        System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextLong()));
         sc.close();
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java b/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java
index d44c30d1..336aeef3 100644
--- a/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java
+++ b/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java
@@ -7,12 +7,27 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 public class BinaryToDecimalTest {
 
     @Test
+    // Test converting binary to decimal
     public void testBinaryToDecimal() {
-        //zeros at the starting should be removed
+        // zeros at the starting should be removed
         assertEquals(0, BinaryToDecimal.binaryToDecimal(0));
         assertEquals(1, BinaryToDecimal.binaryToDecimal(1));
         assertEquals(5, BinaryToDecimal.binaryToDecimal(101));
         assertEquals(63, BinaryToDecimal.binaryToDecimal(111111));
         assertEquals(512, BinaryToDecimal.binaryToDecimal(1000000000));
     }
+
+    @Test
+    // Test converting negative binary numbers
+    public void testNegativeBinaryToDecimal() {
+        assertEquals(-1, BinaryToDecimal.binaryToDecimal(-1));
+        assertEquals(-42, BinaryToDecimal.binaryToDecimal(-101010));
+    }
+
+    @Test
+    // Test converting binary numbers with large values
+    public void testLargeBinaryToDecimal() {
+        assertEquals(262144L, BinaryToDecimal.binaryToDecimal(1000000000000000000L));
+        assertEquals(524287L, BinaryToDecimal.binaryToDecimal(1111111111111111111L));
+    }
 }
\ No newline at end of file