Conversión de decimal a binario sin usar operadores aritméticos

Encuentre el equivalente binario del número no negativo dado n sin usar operadores aritméticos.
Ejemplos: 
 

Input : n = 10
Output : 1010

Input : n = 38
Output : 100110

Tenga en cuenta que + en el algoritmo/programa a continuación se usa con fines de concatenación. 
Algoritmo: 
 

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++

// 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 operators
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
int main()
{
    int n = 38;
    cout << decToBin(n);
    return 0;
}

Java

// Java implementation of decimal
// to binary conversion without
// using arithmetic operators
import 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

# Python3 implementation of
# decimal to binary conversion
# without using arithmetic operators
 
# function for decimal to
# binary conversion without
# using arithmetic operators
def 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 Code
n = 38;
print(decToBin(n));
 
# This code is contributed
# by mits

C#

// C# implementation of decimal
// to binary conversion without
// using arithmetic operators
using 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
// PHP implementation of decimal
// to binary conversion without
// using arithmetic operators
 
// function for decimal to
// binary conversion without
// using arithmetic operators
function 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
?>

Javascript

<script>
// javascript implementation of decimal
// to binary conversion without
// using arithmetic operators
    
// function for decimal to
// binary conversion without
// using arithmetic operators
function 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 above
var n = 38;
document.write(decToBin(n));
 
// This code is contributed by shikhasingrajput
</script>

Producción: 
 

100110

Complejidad temporal: O(num), donde num es el número de bits en la representación binaria de n .
Este artículo es una contribución de Ayush Jauhari . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a contribuya@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *