Decimal to Binary Conversion
Manual Conversion Method
Step-by-Step Algorithm
The manual conversion from decimal to binary involves repeatedly dividing the number by 2 and tracking the remainders:
graph TD
A[Start with Decimal Number] --> B[Divide by 2]
B --> C[Record Remainder]
C --> D{Quotient > 0?}
D -->|Yes| B
D -->|No| E[Read Remainders Bottom-Up]
Conversion Example
Let's convert decimal 13 to binary:
Step |
Operation |
Quotient |
Remainder |
1 |
13 ÷ 2 |
6 |
1 |
2 |
6 ÷ 2 |
3 |
0 |
3 |
3 ÷ 2 |
1 |
1 |
4 |
1 ÷ 2 |
0 |
1 |
Result: 13 in binary is 1101
Programmatic Conversion in Java
Integer.toBinaryString() Method
public class DecimalToBinaryDemo {
public static void main(String[] args) {
int decimal = 13;
String binary = Integer.toBinaryString(decimal);
System.out.println(decimal + " in binary: " + binary);
}
}
Manual Conversion Algorithm
public class ManualBinaryConversion {
public static String convertToBinary(int decimal) {
if (decimal == 0) return "0";
StringBuilder binary = new StringBuilder();
while (decimal > 0) {
binary.insert(0, decimal % 2);
decimal /= 2;
}
return binary.toString();
}
public static void main(String[] args) {
int number = 13;
System.out.println(number + " in binary: " + convertToBinary(number));
}
}
Conversion Considerations
- Works for non-negative integers
- Limited by integer range in Java
- Performance varies with conversion method
At LabEx, we recommend understanding both manual and programmatic conversion techniques for comprehensive binary number manipulation.