Decimal to binary conversion without using arithmetic operators - GeeksforGeeks

06 Apr, 2021
Find the binary equivalent of the given non-negative number n without using arithmetic operators.Examples:  
Input : n = 10
Output : 1010

Input : n = 38
Output : 100110
 
Note that + in below algorithm/program is used for concatenation purpose. Algorithm:  
decToBin(n)
    if n == 0
        return "0"
    Declare bin = ""
    Declare ch
    while n > 0
        if (n & 1) == 0
            ch = '0'
        else
            ch = '1'
        bin = ch + bin
        n = n >> 1
    return bin
 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of decimal to binary conversion// without using arithmetic operators#include <bits/stdc++.h> using namespace std; // function for decimal to binary conversion// without using arithmetic operatorsstring decToBin(int n){    if (n == 0)        return "0";         // to store the binary equivalent of decimal    string bin = "";       while (n > 0)       {        // to get the last binary digit of the number 'n'        // and accumulate it at the beginning of 'bin'        bin = ((n & 1) == 0 ? '0' : '1') + bin;                 // right shift 'n' by 1        n >>= 1;    }         // required binary number    return bin;} // Driver program to test aboveint main(){    int n = 38;    cout << decToBin(n);    return 0;}
// Java implementation of decimal// to binary conversion without// using arithmetic operatorsimport java.io.*; class GFG {         // function for decimal to    // binary conversion without    // using arithmetic operators    static String decToBin(int n)    {        if (n == 0)            return "0";             // to store the binary        // equivalent of decimal        String bin = "";                 while (n > 0)        {            // to get the last binary digit            // of the number 'n' and accumulate            // it at the beginning of 'bin'            bin = ((n & 1) == 0 ? '0' : '1') + bin;                         // right shift 'n' by 1            n >>= 1;        }                 // required binary number        return bin;    }     // Driver program to test above    public static void main (String[] args) {     int n = 38;    System.out.println(decToBin(n));    }} // This code is contributed by vt_m
# Python3 implementation of# decimal to binary conversion# without using arithmetic operators # function for decimal to# binary conversion without# using arithmetic operatorsdef decToBin(n):    if(n == 0):        return "0";             # to store the binary    # equivalent of decimal    bin = "";         while (n > 0):                 # to get the last binary        # digit of the number 'n'        # and accumulate it at        # the beginning of 'bin'        if (n & 1 == 0):            bin = '0' + bin;        else:            bin = '1' + bin;                 # right shift 'n' by 1        n = n >> 1;         # required binary number    return bin; # Driver Coden = 38;print(decToBin(n)); # This code is contributed# by mits
// C# implementation of decimal// to binary conversion without// using arithmetic operatorsusing System; class GFG {         // function for decimal to    // binary conversion without    // using arithmetic operators    static String decToBin(int n)    {        if (n == 0)            return "0";         // to store the binary        // equivalent of decimal        String bin = "";         while (n > 0) {                         // to get the last binary digit            // of the number 'n' and accumulate            // it at the beginning of 'bin'            bin = ((n & 1) == 0 ? '0' : '1') + bin;             // right shift 'n' by 1            n >>= 1;        }         // required binary number        return bin;    }     // Driver program to test above    public static void Main()    {         int n = 38;        Console.WriteLine(decToBin(n));    }} // This code is contributed by Sam007
<?php// PHP implementation of decimal// to binary conversion without// using arithmetic operators // function for decimal to// binary conversion without// using arithmetic operatorsfunction decToBin($n){    if ($n == 0)        return "0";         // to store the binary    // equivalent of decimal    $bin = "";    while ($n > 0)    {        // to get the last binary        // digit of the number 'n'        // and accumulate it at        // the beginning of 'bin'        $bin = (($n & 1) == 0 ?                          '0' : '1') . $bin;                 // right shift 'n' by 1        $n >>= 1;    }         // required binary number    return $bin;} // Driver Code$n = 38;echo decToBin($n); // This code is contributed// by mits?>
<script>// javascript implementation of decimal// to binary conversion without// using arithmetic operators    // function for decimal to// binary conversion without// using arithmetic operatorsfunction decToBin(n){    if (n == 0)        return "0";     // to store the binary    // equivalent of decimal    var bin = "";         while (n > 0)    {        // to get the last binary digit        // of the number 'n' and accumulate        // it at the beginning of 'bin'        bin = ((n & 1) == 0 ? '0' : '1') + bin;                 // right shift 'n' by 1        n >>= 1;    }         // required binary number    return bin;} // Driver program to test abovevar n = 38;document.write(decToBin(n)); // This code is contributed by shikhasingrajput</script>
Output:  
100110
Time complexity: O(num), where num is the number of bits in the binary representation of n.This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Mithun Kumar
shikhasingrajput
base-conversion
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Count ways to reach the n'th stair
Add two numbers without using arithmetic operators
Program to add two binary strings
Program to convert a given number to words
Modular multiplicative inverse