/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/conversions/DecimalToBinary.java
49 строк
2 KB
Alex Klymenko
refactor: `DecimalToBinary` (#5339)
17 авг 2024, 21:35
Не верифицирован
17 авг 2024, 21:35
d80fd0c
Код
Авторство
О чём код?
package com.thealgorithms.conversions; /** * This class provides methods to convert a decimal number to a binary number. */ final class DecimalToBinary { private static final int BINARY_BASE = 2; private static final int DECIMAL_MULTIPLIER = 10; private DecimalToBinary() { } /** * Converts a decimal number to a binary number using a conventional algorithm. * @param decimalNumber the decimal number to convert * @return the binary representation of the decimal number */ public static int convertUsingConventionalAlgorithm(int decimalNumber) { int binaryNumber = 0; int position = 1; while (decimalNumber > 0) { int remainder = decimalNumber % BINARY_BASE; binaryNumber += remainder * position; position *= DECIMAL_MULTIPLIER; decimalNumber /= BINARY_BASE; } return binaryNumber; } /** * Converts a decimal number to a binary number using a bitwise algorithm. * @param decimalNumber the decimal number to convert * @return the binary representation of the decimal number */ public static int convertUsingBitwiseAlgorithm(int decimalNumber) { int binaryNumber = 0; int position = 1; while (decimalNumber > 0) { int leastSignificantBit = decimalNumber & 1; binaryNumber += leastSignificantBit * position; position *= DECIMAL_MULTIPLIER; decimalNumber >>= 1; } return binaryNumber; } }