From d85f192421bbbc0728aaec476b84974942588a10 Mon Sep 17 00:00:00 2001 From: Hardik Pawar <97388607+Hardvan@users.noreply.github.com> Date: Wed, 23 Oct 2024 12:03:00 +0530 Subject: [PATCH] Enhance docs, add more tests in `OctalToBinary` (#5942) --- .../conversions/OctalToBinary.java | 45 +++++++++++++++++-- .../conversions/OctalToBinaryTest.java | 20 +++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/conversions/OctalToBinary.java b/src/main/java/com/thealgorithms/conversions/OctalToBinary.java index 6b01c2f65..a66db9763 100644 --- a/src/main/java/com/thealgorithms/conversions/OctalToBinary.java +++ b/src/main/java/com/thealgorithms/conversions/OctalToBinary.java @@ -1,14 +1,40 @@ package com.thealgorithms.conversions; /** - * Converts any Octal Number to a Binary Number + * A utility class to convert an octal (base-8) number into its binary (base-2) representation. + * + *
This class provides methods to: + *
An octal number is converted to binary by converting each octal digit to its 3-bit binary equivalent. + * The result is a long representing the full binary equivalent of the octal number.
+ * + *+ * long binary = OctalToBinary.convertOctalToBinary(52); // Output: 101010 (52 in octal is 101010 in binary) + ** * @author Bama Charan Chhandogi + * @see Octal Number System + * @see Binary Number System */ - public final class OctalToBinary { private OctalToBinary() { } + + /** + * Converts an octal number to its binary representation. + * + *
Each octal digit is individually converted to its 3-bit binary equivalent, and the binary + * digits are concatenated to form the final binary number.
+ * + * @param octalNumber the octal number to convert (non-negative integer) + * @return the binary equivalent as a long + */ public static long convertOctalToBinary(int octalNumber) { long binaryNumber = 0; int digitPosition = 1; @@ -20,12 +46,25 @@ public final class OctalToBinary { binaryNumber += binaryDigit * digitPosition; octalNumber /= 10; - digitPosition *= 1000; // Move to the next group of 3 binary digits + digitPosition *= 1000; } return binaryNumber; } + /** + * Converts a single octal digit (0-7) to its binary equivalent. + * + *For example: + *