Skip to content

Commit e7a470b

Browse files
committed
refactor: Enhance docs, add more tests in OctalToBinary
1 parent 2f9f75a commit e7a470b

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/main/java/com/thealgorithms/conversions/OctalToBinary.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
package com.thealgorithms.conversions;
22

33
/**
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);
19+
* // Output: 101010 (52 in octal is 101010 in binary)
20+
* </pre>
521
*
622
* @author Bama Charan Chhandogi
23+
* @see <a href="https://en.wikipedia.org/wiki/Octal">Octal Number System</a>
24+
* @see <a href="https://en.wikipedia.org/wiki/Binary_number">Binary Number System</a>
725
*/
8-
926
public final class OctalToBinary {
1027
private OctalToBinary() {
1128
}
29+
30+
/**
31+
* Converts an octal number to its binary representation.
32+
*
33+
* <p>Each octal digit is individually converted to its 3-bit binary equivalent, and the binary
34+
* digits are concatenated to form the final binary number.</p>
35+
*
36+
* @param octalNumber the octal number to convert (non-negative integer)
37+
* @return the binary equivalent as a long
38+
*/
1239
public static long convertOctalToBinary(int octalNumber) {
1340
long binaryNumber = 0;
1441
int digitPosition = 1;
@@ -26,6 +53,19 @@ public static long convertOctalToBinary(int octalNumber) {
2653
return binaryNumber;
2754
}
2855

56+
/**
57+
* Converts a single octal digit (0-7) to its binary equivalent.
58+
*
59+
* <p>For example:
60+
* <ul>
61+
* <li>Octal digit 7 is converted to binary 111</li>
62+
* <li>Octal digit 3 is converted to binary 011</li>
63+
* </ul>
64+
* </p>
65+
*
66+
* @param octalDigit a single octal digit (0-7)
67+
* @return the binary equivalent as a long
68+
*/
2969
public static long convertOctalDigitToBinary(int octalDigit) {
3070
long binaryDigit = 0;
3171
int binaryMultiplier = 1;

src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,24 @@ public void testConvertOctalToBinary() {
1212
assertEquals(101010, OctalToBinary.convertOctalToBinary(52));
1313
assertEquals(110, OctalToBinary.convertOctalToBinary(6));
1414
}
15+
16+
@Test
17+
public void testConvertOctalToBinarySingleDigit() {
18+
assertEquals(0, OctalToBinary.convertOctalToBinary(0));
19+
assertEquals(1, OctalToBinary.convertOctalToBinary(1));
20+
assertEquals(111, OctalToBinary.convertOctalToBinary(7));
21+
}
22+
23+
@Test
24+
public void testConvertOctalToBinaryMultipleDigits() {
25+
assertEquals(100110111, OctalToBinary.convertOctalToBinary(467));
26+
assertEquals(111101, OctalToBinary.convertOctalToBinary(75));
27+
assertEquals(111100101, OctalToBinary.convertOctalToBinary(745));
28+
}
29+
30+
@Test
31+
public void testConvertOctalToBinaryWithZeroPadding() {
32+
assertEquals(100001010, OctalToBinary.convertOctalToBinary(412));
33+
assertEquals(101101110, OctalToBinary.convertOctalToBinary(556));
34+
}
1535
}

0 commit comments

Comments
 (0)