1
1
package com .thealgorithms .conversions ;
2
2
3
- import java .util .Scanner ;
4
-
5
3
/**
6
- * This class converts a Decimal number to a Binary number
4
+ * This class provides methods to convert a decimal number to a binary number.
7
5
*/
8
6
final class DecimalToBinary {
7
+ private static final int BINARY_BASE = 2 ;
8
+ private static final int DECIMAL_MULTIPLIER = 10 ;
9
+
9
10
private DecimalToBinary () {
10
11
}
11
12
12
13
/**
13
- * Main Method
14
- *
15
- * @param args Command Line Arguments
14
+ * Converts a decimal number to a binary number using a conventional algorithm.
15
+ * @param decimalNumber the decimal number to convert
16
+ * @return the binary representation of the decimal number
16
17
*/
17
- public static void main (String [] args ) {
18
- conventionalConversion ();
19
- bitwiseConversion ();
20
- }
18
+ public static int convertUsingConventionalAlgorithm (int decimalNumber ) {
19
+ int binaryNumber = 0 ;
20
+ int position = 1 ;
21
21
22
- /**
23
- * This method converts a decimal number to a binary number using a
24
- * conventional algorithm.
25
- */
26
- public static void conventionalConversion () {
27
- int n ;
28
- int b = 0 ;
29
- int c = 0 ;
30
- int d ;
31
- Scanner input = new Scanner (System .in );
32
- System .out .printf ("Conventional conversion.%n Enter the decimal number: " );
33
- n = input .nextInt ();
34
- while (n != 0 ) {
35
- d = n % 2 ;
36
- b = b + d * (int ) Math .pow (10 , c ++);
37
- n /= 2 ;
38
- } // converting decimal to binary
39
- System .out .println ("\t Binary number: " + b );
40
- input .close ();
22
+ while (decimalNumber > 0 ) {
23
+ int remainder = decimalNumber % BINARY_BASE ;
24
+ binaryNumber += remainder * position ;
25
+ position *= DECIMAL_MULTIPLIER ;
26
+ decimalNumber /= BINARY_BASE ;
27
+ }
28
+
29
+ return binaryNumber ;
41
30
}
42
31
43
32
/**
44
- * This method converts a decimal number to a binary number using a bitwise
45
- * algorithm
33
+ * Converts a decimal number to a binary number using a bitwise algorithm.
34
+ * @param decimalNumber the decimal number to convert
35
+ * @return the binary representation of the decimal number
46
36
*/
47
- public static void bitwiseConversion () {
48
- int n ;
49
- int b = 0 ;
50
- int c = 0 ;
51
- int d ;
52
- Scanner input = new Scanner (System .in );
53
- System .out .printf ("Bitwise conversion.%n Enter the decimal number: " );
54
- n = input .nextInt ();
55
- while (n != 0 ) {
56
- d = (n & 1 );
57
- b += d * (int ) Math .pow (10 , c ++);
58
- n >>= 1 ;
37
+ public static int convertUsingBitwiseAlgorithm (int decimalNumber ) {
38
+ int binaryNumber = 0 ;
39
+ int position = 1 ;
40
+
41
+ while (decimalNumber > 0 ) {
42
+ int leastSignificantBit = decimalNumber & 1 ;
43
+ binaryNumber += leastSignificantBit * position ;
44
+ position *= DECIMAL_MULTIPLIER ;
45
+ decimalNumber >>= 1 ;
59
46
}
60
- System .out .println ("\t Binary number: " + b );
61
- input .close ();
47
+ return binaryNumber ;
62
48
}
63
- }
49
+ }
0 commit comments