Skip to content

Commit d86413f

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents 30a47f8 + 404ad72 commit d86413f

File tree

10 files changed

+301
-205
lines changed

10 files changed

+301
-205
lines changed
Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,45 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
5-
/**
6-
* Converts any Binary number to an Octal Number
7-
*
8-
* @author Zachary Jones
9-
*/
103
public final class BinaryToOctal {
11-
private BinaryToOctal() {
12-
}
4+
private static final int BITS_PER_OCTAL_DIGIT = 3;
5+
private static final int BINARY_BASE = 2;
6+
private static final int DECIMAL_BASE = 10;
137

14-
/**
15-
* Main method
16-
*
17-
* @param args Command line arguments
18-
*/
19-
public static void main(String[] args) {
20-
Scanner sc = new Scanner(System.in);
21-
System.out.println("Input the binary number: ");
22-
int b = sc.nextInt();
23-
System.out.println("Octal equivalent: " + convertBinaryToOctal(b));
24-
sc.close();
8+
private BinaryToOctal() {
259
}
2610

2711
/**
2812
* This method converts a binary number to an octal number.
2913
*
3014
* @param binary The binary number
3115
* @return The octal number
16+
* @throws IllegalArgumentException if the input is not a valid binary number
3217
*/
3318
public static String convertBinaryToOctal(int binary) {
34-
String octal = "";
35-
int currBit = 0;
36-
int j = 1;
19+
if (binary == 0) {
20+
return "0";
21+
}
22+
23+
if (!String.valueOf(binary).matches("[01]+")) {
24+
throw new IllegalArgumentException("Input is not a valid binary number.");
25+
}
26+
27+
StringBuilder octal = new StringBuilder();
28+
int currentBit;
29+
int bitValueMultiplier = 1;
30+
3731
while (binary != 0) {
38-
int code3 = 0;
39-
for (int i = 0; i < 3; i++) {
40-
currBit = binary % 10;
41-
binary = binary / 10;
42-
code3 += currBit * j;
43-
j *= 2;
32+
int octalDigit = 0;
33+
for (int i = 0; i < BITS_PER_OCTAL_DIGIT && binary != 0; i++) {
34+
currentBit = binary % DECIMAL_BASE;
35+
binary /= DECIMAL_BASE;
36+
octalDigit += currentBit * bitValueMultiplier;
37+
bitValueMultiplier *= BINARY_BASE;
4438
}
45-
octal = code3 + octal;
46-
j = 1;
39+
octal.insert(0, octalDigit);
40+
bitValueMultiplier = 1; // Reset multiplier for the next group
4741
}
48-
return octal;
42+
43+
return octal.toString();
4944
}
5045
}
Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,49 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
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.
75
*/
86
final class DecimalToBinary {
7+
private static final int BINARY_BASE = 2;
8+
private static final int DECIMAL_MULTIPLIER = 10;
9+
910
private DecimalToBinary() {
1011
}
1112

1213
/**
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
1617
*/
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;
2121

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("\tBinary 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;
4130
}
4231

4332
/**
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
4636
*/
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;
5946
}
60-
System.out.println("\tBinary number: " + b);
61-
input.close();
47+
return binaryNumber;
6248
}
6349
}

src/main/java/com/thealgorithms/conversions/DecimalToHexaDecimal.java

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.thealgorithms.conversions;
2+
3+
/**
4+
* This class provides a method to convert a decimal number to a hexadecimal string.
5+
*/
6+
final class DecimalToHexadecimal {
7+
private static final int SIZE_OF_INT_IN_HALF_BYTES = 8;
8+
private static final int NUMBER_OF_BITS_IN_HALF_BYTE = 4;
9+
private static final int HALF_BYTE_MASK = 0x0F;
10+
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
11+
12+
private DecimalToHexadecimal() {
13+
}
14+
15+
/**
16+
* Converts a decimal number to a hexadecimal string.
17+
* @param decimal the decimal number to convert
18+
* @return the hexadecimal representation of the decimal number
19+
*/
20+
public static String decToHex(int decimal) {
21+
StringBuilder hexBuilder = new StringBuilder(SIZE_OF_INT_IN_HALF_BYTES);
22+
for (int i = SIZE_OF_INT_IN_HALF_BYTES - 1; i >= 0; --i) {
23+
int currentHalfByte = decimal & HALF_BYTE_MASK;
24+
hexBuilder.insert(0, HEX_DIGITS[currentHalfByte]);
25+
decimal >>= NUMBER_OF_BITS_IN_HALF_BYTE;
26+
}
27+
return removeLeadingZeros(hexBuilder.toString().toLowerCase());
28+
}
29+
30+
private static String removeLeadingZeros(String str) {
31+
if (str == null || str.isEmpty()) {
32+
return str;
33+
}
34+
35+
int i = 0;
36+
while (i < str.length() && str.charAt(i) == '0') {
37+
i++;
38+
}
39+
40+
return i == str.length() ? "0" : str.substring(i);
41+
}
42+
}

0 commit comments

Comments
 (0)