First and Last Three Bits | Function to print the first and last 3 bits equivalent decimal number ; Converting n to binary ; Length of the array has to be at least 3 ; Convert first three bits to decimal ; Print the decimal ; Convert last three bits to decimal ; Print the decimal ; Driver code
function binToDecimal3 ( n ) { var a = Array ( 64 ) . fill ( 0 ) ; var x = 0 , i ; for ( i = 0 ; n > 0 ; i ++ ) { a [ i ] = n % 2 ; n = parseInt ( n / 2 ) ; } x = ( i < 3 ) ? 3 : i ; var d = 0 , p = 0 ; for ( var i = x - 3 ; i < x ; i ++ ) d += a [ i ] * parseInt ( Math . pow ( 2 , p ++ ) ) ; document . write ( d + " " ) ; d = 0 ; p = 0 ; for ( var i = 0 ; i < 3 ; i ++ ) d += a [ i ] * parseInt ( Math . pow ( 2 , p ++ ) ) ; document . write ( d ) ; } var n = 86 ; binToDecimal3 ( n ) ;