mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 14:04:17 +08:00
44 lines
1.1 KiB
Java
44 lines
1.1 KiB
Java
package com.thealgorithms.conversions;
|
|
|
|
/**
|
|
* Converts any Octal Number to a Binary Number
|
|
*
|
|
* @author Bama Charan Chhandogi
|
|
*/
|
|
|
|
public final class OctalToBinary {
|
|
private OctalToBinary() {
|
|
}
|
|
public static long convertOctalToBinary(int octalNumber) {
|
|
long binaryNumber = 0;
|
|
int digitPosition = 1;
|
|
|
|
while (octalNumber != 0) {
|
|
int octalDigit = octalNumber % 10;
|
|
long binaryDigit = convertOctalDigitToBinary(octalDigit);
|
|
|
|
binaryNumber += binaryDigit * digitPosition;
|
|
|
|
octalNumber /= 10;
|
|
digitPosition *= 1000; // Move to the next group of 3 binary digits
|
|
}
|
|
|
|
return binaryNumber;
|
|
}
|
|
|
|
public static long convertOctalDigitToBinary(int octalDigit) {
|
|
long binaryDigit = 0;
|
|
int binaryMultiplier = 1;
|
|
|
|
while (octalDigit != 0) {
|
|
int octalDigitRemainder = octalDigit % 2;
|
|
binaryDigit += octalDigitRemainder * binaryMultiplier;
|
|
|
|
octalDigit /= 2;
|
|
binaryMultiplier *= 10;
|
|
}
|
|
|
|
return binaryDigit;
|
|
}
|
|
}
|