Find the number obtained after concatenation of binary representation of M and N

Given two integers M and N the task is to find the number formed by concatenating the binary equivalents of M and N i.e. M + N.

Examples:

Input: M = 4, N = 5
Output: 37
Binary equivalent of 4 is 100 and for 5 it is 101
after concatenation, the resultant binary number
formed is 100101 whose decimal equivalent is 37.



Input: M = 3, N = 4
Output: 28

Approach: Convert both the numbers M and N into there binary equivalents then concatenate these numbers as M + N and print the decimal equivalent of the resulting binary number formed after concatenation.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to convert decimal number n
// to its binary representation
// stored as an array arr[]
void decBinary(int arr[], int n)
{
    int k = log2(n);
    while (n > 0) {
        arr[k--] = n % 2;
        n /= 2;
    }
}
  
// Funtion to convert the number
// represented as a binary array
// arr[] into its decimal equivalent
int binaryDec(int arr[], int n)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += arr[i] << (n - i - 1);
    return ans;
}
  
// Function to concatenate the binary
// numbers and return the decimal result
int concat(int m, int n)
{
  
    // Number of bits in both the numbers
    int k = log2(m) + 1;
    int l = log2(n) + 1;
  
    // Convert the bits in both the integers
    // to the arrays a[] and b[]
    int a[k] = { 0 }, b[l] = { 0 };
  
    // c[] will be the binary array
    // for the result
    int c[k + l] = { 0 };
    decBinary(a, m);
    decBinary(b, n);
  
    // Update the c[] array
    int in = 0;
    for (int i = 0; i < k; i++)
        c[in++] = a[i];
    for (int i = 0; i < l; i++)
        c[in++] = b[i];
  
    // Return the decimal equivalent
    // of the result
    return (binaryDec(c, k + l));
}
  
// Driver code
int main()
{
    int m = 4, n = 5;
  
    cout << concat(m, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
{
      
    // Function to convert decimal number n 
    // to its binary representation 
    // stored as an array arr[] 
    static void decBinary(int arr[], int n) 
    
        int k = (int)(Math.log(n) /
                      Math.log(2)); 
          
        while (n > 0)
        
            arr[k--] = n % 2
            n /= 2
        
    
      
    // Funtion to convert the number 
    // represented as a binary array 
    // arr[] into its decimal equivalent 
    static int binaryDec(int arr[], int n) 
    
        int ans = 0
        for (int i = 0; i < n; i++) 
            ans += arr[i] << (n - i - 1); 
        return ans; 
    
      
    // Function to concatenate the binary 
    // numbers and return the decimal result 
    static int concat(int m, int n) 
    
      
        // Number of bits in both the numbers 
        int k = (int)(Math.log(m) / 
                      Math.log(2)) + 1
        int l = (int)(Math.log(n) /
                      Math.log(2)) + 1
      
        // Convert the bits in both the integers 
        // to the arrays a[] and b[] 
        int a[] = new int[k];
        int b[] = new int[l]; 
      
        // c[] will be the binary array 
        // for the result 
        int c[] = new int[k + l]; 
        decBinary(a, m); 
        decBinary(b, n); 
      
        // Update the c[] array 
        int in = 0
        for (int i = 0; i < k; i++) 
            c[in++] = a[i]; 
        for (int i = 0; i < l; i++) 
            c[in++] = b[i]; 
      
        // Return the decimal equivalent 
        // of the result 
        return (binaryDec(c, k + l)); 
    
      
    // Driver code 
    public static void main (String[] args)
    
        int m = 4, n = 5
      
        System.out.println(concat(m, n)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the approach 
import math
  
# Function to convert decimal number n 
# to its binary representation 
# stored as an array arr[] 
def decBinary(arr, n):
    k = int(math.log2(n)) 
    while (n > 0):
        arr[k] = n % 2
        k = k - 1
        n = n//2
  
# Funtion to convert the number 
# represented as a binary array 
# arr[] its decimal equivalent 
def binaryDec(arr, n):
    ans = 0
    for i in range(0, n):
        ans = ans + (arr[i] << (n - i - 1))
    return ans
  
# Function to concatenate the binary 
# numbers and return the decimal result 
def concat(m, n): 
  
    # Number of bits in both the numbers 
    k = int(math.log2(m)) + 1
    l = int(math.log2(n)) + 1
  
    # Convert the bits in both the gers 
    # to the arrays a[] and b[] 
    a = [0 for i in range(0, k)]
    b = [0 for i in range(0, l)]
  
    # c[] will be the binary array 
    # for the result 
    c = [0 for i in range(0, k + l)]
    decBinary(a, m); 
    decBinary(b, n); 
  
    # Update the c[] array 
    iin = 0
    for i in range(0, k): 
        c[iin] = a[i]
        iin = iin + 1
    for i in range(0, l): 
        c[iin] = b[i]
        iin = iin + 1
  
    # Return the decimal equivalent 
    # of the result 
    return (binaryDec(c, k + l))
  
# Driver code 
m = 4
n = 5
  
print(concat(m, n))
  
# This code is contributed by Sanjit_Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG 
{
      
    // Function to convert decimal number n 
    // to its binary representation 
    // stored as an array []arr 
    static void decBinary(int []arr, int n) 
    
        int k = (int)(Math.Log(n) /
                      Math.Log(2)); 
          
        while (n > 0)
        
            arr[k--] = n % 2; 
            n /= 2; 
        
    
      
    // Funtion to convert the number 
    // represented as a binary array 
    // []arr into its decimal equivalent 
    static int binaryDec(int []arr, int n) 
    
        int ans = 0; 
        for (int i = 0; i < n; i++) 
            ans += arr[i] << (n - i - 1); 
        return ans; 
    
      
    // Function to concatenate the binary 
    // numbers and return the decimal result 
    static int concat(int m, int n) 
    
      
        // Number of bits in both the numbers 
        int k = (int)(Math.Log(m) / 
                      Math.Log(2)) + 1; 
        int l = (int)(Math.Log(n) /
                      Math.Log(2)) + 1; 
      
        // Convert the bits in both the integers 
        // to the arrays []a and []b 
        int []a = new int[k];
        int []b = new int[l]; 
      
        // c[] will be the binary array 
        // for the result 
        int []c = new int[k + l]; 
        decBinary(a, m); 
        decBinary(b, n); 
      
        // Update the c[] array 
        int iN = 0; 
        for (int i = 0; i < k; i++) 
            c[iN++] = a[i]; 
        for (int i = 0; i < l; i++) 
            c[iN++] = b[i]; 
      
        // Return the decimal equivalent 
        // of the result 
        return (binaryDec(c, k + l)); 
    
      
    // Driver code 
    public static void Main(String[] args)
    
        int m = 4, n = 5; 
      
        Console.WriteLine(concat(m, n)); 
    
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

37



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.