1
1
package com .thealgorithms .conversions ;
2
2
3
3
/**
4
- * Converts any Octal Number to a Binary Number
4
+ * A utility class to convert an octal (base-8) number into its binary (base-2) representation.
5
+ *
6
+ * <p>This class provides methods to:
7
+ * <ul>
8
+ * <li>Convert an octal number to its binary equivalent</li>
9
+ * <li>Convert individual octal digits to binary</li>
10
+ * </ul>
11
+ *
12
+ * <h2>Octal to Binary Conversion:</h2>
13
+ * <p>An octal number is converted to binary by converting each octal digit to its 3-bit binary equivalent.
14
+ * The result is a long representing the full binary equivalent of the octal number.</p>
15
+ *
16
+ * <h2>Example Usage</h2>
17
+ * <pre>
18
+ * long binary = OctalToBinary.convertOctalToBinary(52); // Output: 101010 (52 in octal is 101010 in binary)
19
+ * </pre>
5
20
*
6
21
* @author Bama Charan Chhandogi
22
+ * @see <a href="https://en.wikipedia.org/wiki/Octal">Octal Number System</a>
23
+ * @see <a href="https://en.wikipedia.org/wiki/Binary_number">Binary Number System</a>
7
24
*/
8
-
9
25
public final class OctalToBinary {
10
26
private OctalToBinary () {
11
27
}
28
+
29
+ /**
30
+ * Converts an octal number to its binary representation.
31
+ *
32
+ * <p>Each octal digit is individually converted to its 3-bit binary equivalent, and the binary
33
+ * digits are concatenated to form the final binary number.</p>
34
+ *
35
+ * @param octalNumber the octal number to convert (non-negative integer)
36
+ * @return the binary equivalent as a long
37
+ */
12
38
public static long convertOctalToBinary (int octalNumber ) {
13
39
long binaryNumber = 0 ;
14
40
int digitPosition = 1 ;
@@ -20,12 +46,25 @@ public static long convertOctalToBinary(int octalNumber) {
20
46
binaryNumber += binaryDigit * digitPosition ;
21
47
22
48
octalNumber /= 10 ;
23
- digitPosition *= 1000 ; // Move to the next group of 3 binary digits
49
+ digitPosition *= 1000 ;
24
50
}
25
51
26
52
return binaryNumber ;
27
53
}
28
54
55
+ /**
56
+ * Converts a single octal digit (0-7) to its binary equivalent.
57
+ *
58
+ * <p>For example:
59
+ * <ul>
60
+ * <li>Octal digit 7 is converted to binary 111</li>
61
+ * <li>Octal digit 3 is converted to binary 011</li>
62
+ * </ul>
63
+ * </p>
64
+ *
65
+ * @param octalDigit a single octal digit (0-7)
66
+ * @return the binary equivalent as a long
67
+ */
29
68
public static long convertOctalDigitToBinary (int octalDigit ) {
30
69
long binaryDigit = 0 ;
31
70
int binaryMultiplier = 1 ;
0 commit comments