refactor: DecimalToBinary (#5339)

This commit is contained in:
Alex Klymenko
2024-08-17 20:35:36 +02:00
committed by GitHub
parent 7c58b190c8
commit d80fd0c623
2 changed files with 56 additions and 49 deletions

View File

@ -1,63 +1,49 @@
package com.thealgorithms.conversions; package com.thealgorithms.conversions;
import java.util.Scanner;
/** /**
* This class converts a Decimal number to a Binary number * This class provides methods to convert a decimal number to a binary number.
*/ */
final class DecimalToBinary { final class DecimalToBinary {
private static final int BINARY_BASE = 2;
private static final int DECIMAL_MULTIPLIER = 10;
private DecimalToBinary() { private DecimalToBinary() {
} }
/** /**
* Main Method * Converts a decimal number to a binary number using a conventional algorithm.
* * @param decimalNumber the decimal number to convert
* @param args Command Line Arguments * @return the binary representation of the decimal number
*/ */
public static void main(String[] args) { public static int convertUsingConventionalAlgorithm(int decimalNumber) {
conventionalConversion(); int binaryNumber = 0;
bitwiseConversion(); int position = 1;
while (decimalNumber > 0) {
int remainder = decimalNumber % BINARY_BASE;
binaryNumber += remainder * position;
position *= DECIMAL_MULTIPLIER;
decimalNumber /= BINARY_BASE;
}
return binaryNumber;
} }
/** /**
* This method converts a decimal number to a binary number using a * Converts a decimal number to a binary number using a bitwise algorithm.
* conventional algorithm. * @param decimalNumber the decimal number to convert
* @return the binary representation of the decimal number
*/ */
public static void conventionalConversion() { public static int convertUsingBitwiseAlgorithm(int decimalNumber) {
int n; int binaryNumber = 0;
int b = 0; int position = 1;
int c = 0;
int d;
Scanner input = new Scanner(System.in);
System.out.printf("Conventional conversion.%n Enter the decimal number: ");
n = input.nextInt();
while (n != 0) {
d = n % 2;
b = b + d * (int) Math.pow(10, c++);
n /= 2;
} // converting decimal to binary
System.out.println("\tBinary number: " + b);
input.close();
}
/** while (decimalNumber > 0) {
* This method converts a decimal number to a binary number using a bitwise int leastSignificantBit = decimalNumber & 1;
* algorithm binaryNumber += leastSignificantBit * position;
*/ position *= DECIMAL_MULTIPLIER;
public static void bitwiseConversion() { decimalNumber >>= 1;
int n;
int b = 0;
int c = 0;
int d;
Scanner input = new Scanner(System.in);
System.out.printf("Bitwise conversion.%n Enter the decimal number: ");
n = input.nextInt();
while (n != 0) {
d = (n & 1);
b += d * (int) Math.pow(10, c++);
n >>= 1;
} }
System.out.println("\tBinary number: " + b); return binaryNumber;
input.close();
} }
} }

View File

@ -0,0 +1,21 @@
package com.thealgorithms.conversions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class DecimalToBinaryTest {
@ParameterizedTest
@CsvSource({"0, 0", "1, 1", "2, 10", "5, 101", "10, 1010", "15, 1111", "100, 1100100"})
void testConvertUsingConventionalAlgorithm(int decimal, int expectedBinary) {
assertEquals(expectedBinary, DecimalToBinary.convertUsingConventionalAlgorithm(decimal));
}
@ParameterizedTest
@CsvSource({"0, 0", "1, 1", "2, 10", "5, 101", "10, 1010", "15, 1111", "100, 1100100"})
void testConvertUsingBitwiseAlgorithm(int decimal, int expectedBinary) {
assertEquals(expectedBinary, DecimalToBinary.convertUsingBitwiseAlgorithm(decimal));
}
}