Decimal to binary

Easy Bit Manipulation Basics Original on GeeksforGeeks

Decimal to binary is a easy bit manipulation problem solved with the basics pattern. The best approach, bit test from the highest set bit, runs in O(log n) time and O(log n) space. Below are 2 approaches in Java, from repeated division up.

Problem

Convert a positive integer n to its binary representation as a string, without using a library conversion.

Examples

Example 1

Input
n = 13
Output
"1101"
Why
8 + 4 + 1.

Example 2

Input
n = 1
Output
"1"

Constraints

  • 1 <= n <= 10^9

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Repeated divisionO(log n)O(log n)
Bit test from the highest set bitO(log n)O(log n)

1Repeated division

TimeO(log n)
SpaceO(log n)

Take n % 2 as the next bit, divide by 2, repeat, then reverse the collected bits.

  1. While n > 0: append n % 2; n /= 2.
  2. Reverse.
Java
class Solution {
    static String decToBinary(int n) {
        if (n == 0) return "0";
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            sb.append(n % 2);
            n /= 2;
        }
        return sb.reverse().toString();
    }
}

2Bit test from the highest set bit

TimeO(log n)
SpaceO(log n)

Find the highest set bit, then test each bit from there down with (n >> i) & 1. No reversal needed.

  1. hi = 31 - Integer.numberOfLeadingZeros(n).
  2. For i from hi down to 0: append (n >> i) & 1.
Java
class Solution {
    static String decToBinary(int n) {
        if (n == 0) return "0";
        StringBuilder sb = new StringBuilder();
        for (int i = 31 - Integer.numberOfLeadingZeros(n); i >= 0; i--) sb.append((n >> i) & 1);
        return sb.toString();
    }
}

Edge cases to test

  • Powers of two (a single 1 followed by zeros)
  • n = 0 if allowed ("0")

Hints

Hint 1

n % 2 is the lowest bit; n / 2 (or n >> 1) drops it. Collect bits from lowest to highest, then reverse.

FAQ

What is the best time complexity for Decimal to binary?

Bit test from the highest set bit runs in O(log n) time and O(log n) extra space.

Which pattern does Decimal to binary use?

It is a bit manipulation problem that uses the basics pattern. Other problems with the same pattern: Convert Binary Number in a Linked List to Integer, K-th Bit is Set or Not, Odd or Even.

Is there a brute force solution for Decimal to binary?

Yes. Repeated division takes O(log n) time and O(log n) space. Take n % 2 as the next bit, divide by 2, repeat, then reverse the collected bits.

Which edge cases should I test for Decimal to binary?

Powers of two (a single 1 followed by zeros); n = 0 if allowed ("0").