mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
Enhance docs, add more tests in OctalToBinary
(#5942)
This commit is contained in:
@ -1,14 +1,40 @@
|
|||||||
package com.thealgorithms.conversions;
|
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.
|
||||||
|
*
|
||||||
|
* <p>This class provides methods to:
|
||||||
|
* <ul>
|
||||||
|
* <li>Convert an octal number to its binary equivalent</li>
|
||||||
|
* <li>Convert individual octal digits to binary</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <h2>Octal to Binary Conversion:</h2>
|
||||||
|
* <p>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.</p>
|
||||||
|
*
|
||||||
|
* <h2>Example Usage</h2>
|
||||||
|
* <pre>
|
||||||
|
* long binary = OctalToBinary.convertOctalToBinary(52); // Output: 101010 (52 in octal is 101010 in binary)
|
||||||
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author Bama Charan Chhandogi
|
* @author Bama Charan Chhandogi
|
||||||
|
* @see <a href="https://en.wikipedia.org/wiki/Octal">Octal Number System</a>
|
||||||
|
* @see <a href="https://en.wikipedia.org/wiki/Binary_number">Binary Number System</a>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public final class OctalToBinary {
|
public final class OctalToBinary {
|
||||||
private OctalToBinary() {
|
private OctalToBinary() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts an octal number to its binary representation.
|
||||||
|
*
|
||||||
|
* <p>Each octal digit is individually converted to its 3-bit binary equivalent, and the binary
|
||||||
|
* digits are concatenated to form the final binary number.</p>
|
||||||
|
*
|
||||||
|
* @param octalNumber the octal number to convert (non-negative integer)
|
||||||
|
* @return the binary equivalent as a long
|
||||||
|
*/
|
||||||
public static long convertOctalToBinary(int octalNumber) {
|
public static long convertOctalToBinary(int octalNumber) {
|
||||||
long binaryNumber = 0;
|
long binaryNumber = 0;
|
||||||
int digitPosition = 1;
|
int digitPosition = 1;
|
||||||
@ -20,12 +46,25 @@ public final class OctalToBinary {
|
|||||||
binaryNumber += binaryDigit * digitPosition;
|
binaryNumber += binaryDigit * digitPosition;
|
||||||
|
|
||||||
octalNumber /= 10;
|
octalNumber /= 10;
|
||||||
digitPosition *= 1000; // Move to the next group of 3 binary digits
|
digitPosition *= 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
return binaryNumber;
|
return binaryNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a single octal digit (0-7) to its binary equivalent.
|
||||||
|
*
|
||||||
|
* <p>For example:
|
||||||
|
* <ul>
|
||||||
|
* <li>Octal digit 7 is converted to binary 111</li>
|
||||||
|
* <li>Octal digit 3 is converted to binary 011</li>
|
||||||
|
* </ul>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param octalDigit a single octal digit (0-7)
|
||||||
|
* @return the binary equivalent as a long
|
||||||
|
*/
|
||||||
public static long convertOctalDigitToBinary(int octalDigit) {
|
public static long convertOctalDigitToBinary(int octalDigit) {
|
||||||
long binaryDigit = 0;
|
long binaryDigit = 0;
|
||||||
int binaryMultiplier = 1;
|
int binaryMultiplier = 1;
|
||||||
|
@ -12,4 +12,24 @@ public class OctalToBinaryTest {
|
|||||||
assertEquals(101010, OctalToBinary.convertOctalToBinary(52));
|
assertEquals(101010, OctalToBinary.convertOctalToBinary(52));
|
||||||
assertEquals(110, OctalToBinary.convertOctalToBinary(6));
|
assertEquals(110, OctalToBinary.convertOctalToBinary(6));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertOctalToBinarySingleDigit() {
|
||||||
|
assertEquals(0, OctalToBinary.convertOctalToBinary(0));
|
||||||
|
assertEquals(1, OctalToBinary.convertOctalToBinary(1));
|
||||||
|
assertEquals(111, OctalToBinary.convertOctalToBinary(7));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertOctalToBinaryMultipleDigits() {
|
||||||
|
assertEquals(100110111, OctalToBinary.convertOctalToBinary(467));
|
||||||
|
assertEquals(111101, OctalToBinary.convertOctalToBinary(75));
|
||||||
|
assertEquals(111100101, OctalToBinary.convertOctalToBinary(745));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertOctalToBinaryWithZeroPadding() {
|
||||||
|
assertEquals(100001010, OctalToBinary.convertOctalToBinary(412));
|
||||||
|
assertEquals(101101110, OctalToBinary.convertOctalToBinary(556));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user