Decimal (base 10) is the everyday number system. Binary (base 2) uses only bits 0 and 1. Conversion means rewriting a decimal integer as a string of bits.
Remember
Divide by 2 → remainder is the next bit (LSB first)
Reverse the remainders → MSB … LSB binary string
Example: 13
13 ÷ 2 = 6 rem 1
6 ÷ 2 = 3 rem 0
3 ÷ 2 = 1 rem 1
1 ÷ 2 = 0 rem 1
Remainders (LSB→MSB): 1 0 1 1
Binary (MSB→LSB): 1101
Decimal
Binary
Check
0
0
special case
13
1101
8+4+1
42
101010
32+8+2
Approach
How to Convert Decimal to Binary in Java
Four clear steps — then pick a remainder loop or an MSB bit scan.
Handle n == 0 by printing 0.
While n > 0, record n % 2 and set n = n / 2.
Print the collected remainders from last to first (MSB … LSB).
Or find the highest set bit and print (n >> i) & 1 downward.
Method
Idea
Time
Best for
Remainder loop
Divide by 2; reverse remainders
O(log n)
Interviews, learning place values
MSB bit scan
Print bits from high to low
O(log n)
No reverse step, bit practice
Library
Integer.toBinaryString(n)
O(log n)
Production code
Pseudocode
Pseudocode
function DecimalToBinary(n):
if n == 0:
print 0
return
bits = empty array
while n > 0:
bits.append(n mod 2)
n = n / 2
for i from length(bits) - 1 down to 0:
print bits[i]
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Operation
Divide by 2; remainder is a bit
First remainder
LSB (rightmost bit)
Zero
Print 0 — do not skip
Bit test
(n >> i) & 1
Library
Integer.toBinaryString(n)
Classic check
13 → 1101
Try it
Live Preview
Enter a nonnegative integer to see the remainder steps and the binary result.
Nonnegative integers (capped at 2147483647). Tap a chip or type a value.
Start with the remainder loop so you can explain every bit. Then use an MSB bit scan when you want to print without reversing an array.
Example 1 — Remainder Loop
Store n % 2 values in an array, then print them from last index to first.
Java
class DecimalToBinary {
static void decimalToBinary(int n) {
int[] bits = new int[32];
int count = 0;
if (n < 0) {
System.out.println("Use a nonnegative integer.");
return;
}
if (n == 0) {
System.out.println("0");
return;
}
while (n > 0) {
bits[count++] = n % 2;
n /= 2;
}
for (int i = count - 1; i >= 0; i--) {
System.out.print(bits[i]);
}
System.out.println();
}
public static void main(String[] args) {
int number = 13;
System.out.print(number + " -> ");
decimalToBinary(number);
}
}
Output
13 -> 1101
How It Works
Reject negatives for this beginner version; print 0 immediately when n is zero.
Each n % 2 is the next bit from the right (LSB first).
n /= 2 shifts toward higher bits until the quotient becomes 0.
Print from count - 1 down to 0 so the string reads MSB to LSB.
For 13, remainders 1,0,1,1 reverse to 1101.
Example 2 — MSB Bit Scan
Find the highest set bit, then print each bit from that position down to 0.
Java
class DecimalToBinaryBits {
static void decimalToBinary(int n) {
if (n < 0) {
System.out.println("Use a nonnegative integer.");
return;
}
if (n == 0) {
System.out.println("0");
return;
}
// Find highest set bit
int i = 31;
while (i > 0 && ((n >> i) & 1) == 0) {
i--;
}
for (; i >= 0; i--) {
System.out.print((n >> i) & 1);
}
System.out.println();
}
public static void main(String[] args) {
decimalToBinary(13); // 1101
decimalToBinary(42); // 101010
decimalToBinary(0); // 0
}
}
Output
1101
101010
0
How It Works
Keep the same nonnegative rule and special-case 0.
Scan from bit 31 downward until you find the first 1 — that is the MSB of the minimal form.
From that index down to 0, print (n >> i) & 1.
No remainder array and no reverse step are required.
Use the remainder loop in interviews when you must show divide-by-2; use the bit scan (or Integer.toBinaryString) when bit tricks are welcome.
Edge Cases & Pitfalls
Check these before calling the solution done.
n = 0
Print 0
A bare while (n > 0) loop produces no output otherwise.
Reverse
LSB comes first
Printing remainders in collection order yields the bits backwards.
Negatives
Define the policy
Tutorials usually require n >= 0. Two’s complement needs a fixed width.
Width
Minimal vs padded
Default output drops leading zeros. Pad only when the problem asks for 8/16/32 bits.
Analysis
Time and Space Complexity
Program
Time
Extra space
Remainder loop
O(log n)
O(log n) for the bit array
MSB bit scan
O(log n)
O(1)
Remember
Key Takeaways
Method: divide by 2; remainders are bits (LSB first).
Reverse: print from last remainder to first for normal binary.
Bit scan: print (n >> i) & 1 from the MSB downward.
Test set: 0 → 0; 13 → 1101; 42 → 101010.
One line: collect n % 2 remainders and reverse them — or scan bits from the MSB.
Frequently Asked Questions
Repeatedly divide by 2 and collect remainders (LSB first), then print them in reverse — or scan bits from the highest set bit down to bit 0. You can also call Integer.toBinaryString(n).
The first remainder is the least significant bit. Printing from last remainder to first gives the usual left-to-right binary string.
Handle n == 0 separately and print 0. A while (n > 0) loop would otherwise produce no output.
Find the highest bit that is set, then walk downward printing (n >> i) & 1 for each position. No array reverse is needed.
Both methods are O(b), where b is the number of output bits — about O(log n) for positive n.