Print binary representation of a given number in C, C++, Java, and Python using built-in methods and custom routines.

For example,

Input:  20
Output: 00000000000000000000000000010100
 
Input:  64
Output: 00000000000000000000000001000000
 
Input:  127
Output: 00000000000000000000000001111111
 
Input:  -1
Output: 11111111111111111111111111111111

Practice this problem

1. Iterative Solution

The idea is to check if i'th bit is set for each position i in the given integer n. If i'th bit is set for position i in integer n, then set i'th bit to 1 in the binary representation of n; otherwise, 0. The i ranges from 0 to 31 for a 32–bit integer.

The expression 1 << (i - 1) gives a number that has only i'th bit set. If we do bitwise AND of this expression with any number n, i.e., n & (1 << (i - 1)), a non-zero value indicates that its i'th bit is set for number n. For instance, consider n = 20 and i = 3. The result is a non-zero value, which indicates that the 3rd bit is set for integer 20.

00010100    &           (n = 20)
00000100                (1 << (3-1))
~~~~~~~~
00000100                non-zero value

The time complexity of this solution is O(c), and no extra space is used. Here, c indicates a constant value. Following is the implementation in C, Java, and Python based on the above idea:

C


Download  Run Code

C++


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
The binary representation of 20 is 00000000000000000000000000010100

2. Recursive Solution

For a given number n, the idea is to recur with the value n/2 and add the remainder of n with 2 (n % 2) to the output string. Terminate the recursion when n becomes 0 and return the output string.

The solution runs in O(log(n)) time and requires O(log(n)) extra space for the call stack. The recurrence relation is T(n) = T(n/2) + 1. However, this works only with positive integers. Following is the C++, Java, and Python program that demonstrates the recursive version:

C++


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
The binary representation of 20 is 10100

3. Using Built-in methods

In C++, we can use the bitset() member function from the std::bitset namespace, which can construct a bitset container object from the integer argument.

In Java, we can use the Integer.toBinaryString() method, which returns the specified integer's string representation.

In Python, we can use the built-in function bin() to convert an integer to a binary string prefixed with 0b. If prefix 0b is not desired, consider using either format() function or f–strings. The f–strings are available in Python starting with v3.6.

C++


Download  Run Code

Output:

The binary representation of 20 is 00010100

Java


Download  Run Code

Output:

The binary representation of 20 is 10100

Python


Download  Run Code

Output:

The binary representation of 20 using built-in function bin(): 0b10100
Using format() function | With prefix '0b': 0b10100 | Without prefix '0b': 10100
Using f–string function | With prefix '0b': 0b10100 | Without prefix '0b': 10100