Write a program to print Binary representation of a given number.
Source: Microsoft Interview Set-3
Method 1: Iterative
For any number, we can check whether its ‘i’th bit is 0(OFF) or 1(ON) by bitwise ANDing it with “2^i” (2 raise to i).
1) Let us take number 'NUM' and we want to check whether it's 0th bit is ON or OFF
bit = 2 ^ 0 (0th bit)
if NUM & bit == 1 means 0th bit is ON else 0th bit is OFF
2) Similarly if we want to check whether 5th bit is ON or OFF
bit = 2 ^ 5 (5th bit)
if NUM & bit == 1 means its 5th bit is ON else 5th bit is OFF.
Let us take unsigned integer (32 bit), which consist of 0-31 bits. To print binary representation of unsigned integer, start from 31th bit, check whether 31th bit is ON or OFF, if it is ON print “1” else print “0”. Now check whether 30th bit is ON or OFF, if it is ON print “1” else print “0”, do this for all bits from 31 to 0, finally we will get binary representation of number.
void bin(unsigned n) { unsigned i; for (i = 1 << 31; i > 0; i = i / 2) (n & i)? printf("1"): printf("0"); } int main(void) { bin(7); printf("\n"); bin(4); } |
Method 2: Recursive
Following is recursive method to print binary representation of ‘NUM’.
step 1) if NUM > 1
a) push NUM on stack
b) recursively call function with 'NUM / 2'
step 2)
a) pop NUM from stack, divide it by 2 and print it's remainder.
C++
void bin(unsigned n) { /* step 1 */ if (n > 1) bin(n/2); /* step 2 */ printf("%d", n % 2); } int main(void) { bin(7); printf("\n"); bin(4); } |
Java
class GFG { static void bin(int n) { /* step 1 */ if (n > 1) bin(n/2); /* step 2 */ System.out.print(n % 2); } //Driver code public static void main(String[] args) { bin(7); System.out.println(); bin(4); } } // This code is contributed // by ChitraNayal |
Python 3
def bin(n) : if n > 1 : bin(n // 2) print(n % 2,end = "") if __name__ == "__main__" : bin(7) print() bin(4) # This code is contributed by ANKITRAI1 |
C#
// C# Program for the binary // representation of a given number using System; class GFG { static void bin(int n) { // step 1 if (n > 1) bin(n / 2); // step 2 Console.Write(n % 2); } // Driver code static public void Main () { bin(7); Console.WriteLine(); bin(4); } } // This code is contributed ajit |
PHP
<?php function bin($n) { /* step 1 */ if ($n > 1) bin($n/2); /* step 2 */ echo ($n % 2); } // Driver code bin(7); echo ("\n"); bin(4); // This code is contributed // by Shivi_Aggarwal ?> |
Output:
111 100
Method 3: Recursive using bitwise operator
Steps to convert decimal number to its binary representation are given below:
step 1: Check n > 0 step 2: Right shift the number by 1 bit and recursive function call step 3: Print the bits of number
C++
// C++ implementation of the approach #include<bits/stdc++.h> using namespace std; // Function to convert decimal // to binary number void bin(unsigned n) { if (n > 1) bin(n>>1); printf("%d", n & 1); } // Driver code int main(void) { bin(131); printf("\n"); bin(3); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to convert decimal // to binary number static void bin(Integer n) { if (n > 1) bin(n>>1); System.out.printf("%d", n & 1); } // Driver code public static void main(String[] args) { bin(131); System.out.printf("\n"); bin(3); } } /*This code is contributed by PrinciRaj1992*/ |
C#
// C# implementation of above approach using System; public class GFG { // Function to convert decimal // to binary number static void bin(int n) { if (n > 1) bin(n>>1); Console.Write(n & 1); } // Driver code public static void Main() { bin(131); Console.WriteLine(); bin(3); } } /*This code is contributed by PrinciRaj1992*/ |
Output:
10000011 11
This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Binary representation of previous number
- Largest number with binary representation is m 1's and m-1 0's
- Next greater number than N with exactly one bit different in binary representation of N
- Number of leading zeros in binary representation of a given number
- Prime Number of Set Bits in Binary Representation | Set 1
- Check if binary representation of a number is palindrome
- Find the n-th number whose binary representation is a palindrome
- Number of mismatching bits in the binary representation of two integers
- Check if actual binary representation of a number is palindrome
- Find consecutive 1s of length >= n in binary representation of a number
- Check if binary representation of a given number and its complement are anagram
- Length of longest consecutive zeroes in the binary representation of a number.
- XOR counts of 0s and 1s in binary representation
- Maximum 0's between two immediate 1's in binary representation
- Find value of k-th bit in binary representation



