Find the number obtained after concatenation of binary representation of M and N | Function to convert decimal number n to its binary representation stored as an array [ ] arr ; Funtion to convert the number represented as a binary array [ ] arr into its decimal equivalent ; Function to concatenate the binary numbers and return the decimal result ; Number of bits in both the numbers ; Convert the bits in both the integers to the arrays [ ] a and [ ] b ; c [ ] will be the binary array for the result ; Update the c [ ] array ; Return the decimal equivalent of the result ; Driver code
function decBinary ( arr , n ) { let k = parseInt ( Math . log ( n ) / Math . log ( 2 ) , 10 ) ; while ( n > 0 ) { arr [ k -- ] = n % 2 ; n = parseInt ( n / 2 , 10 ) ; } } function binaryDec ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) ans += arr [ i ] << ( n - i - 1 ) ; return ans ; } function concat ( m , n ) { let k = parseInt ( Math . log ( m ) / Math . log ( 2 ) , 10 ) + 1 ; let l = parseInt ( Math . log ( n ) / Math . log ( 2 ) , 10 ) + 1 ; let a = new Array ( k ) ; let b = new Array ( l ) ; let c = new Array ( k + l ) ; decBinary ( a , m ) ; decBinary ( b , n ) ; let iN = 0 ; for ( let i = 0 ; i < k ; i ++ ) c [ iN ++ ] = a [ i ] ; for ( let i = 0 ; i < l ; i ++ ) c [ iN ++ ] = b [ i ] ; return ( binaryDec ( c , k + l ) ) ; } let m = 4 , n = 5 ; document . write ( concat ( m , n ) ) ;