String Conversion from Decimal to Binary with bit operation - Javascript Operator

Javascript examples for Operator:Bitwise Operator

Description

String Conversion from Decimal to Binary with bit operation

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <input type="number" onchange="set(this)" min="0" max="255" value="0">
      <br> 
      <input type="number" readonly id="out"> 
      <script>
function decbin(dec,length){// w  w w  . j  a v  a  2s . com
  var out = "";
  while(length--)
    out += (dec >> length ) & 1;
  return out;
}
function set(e){
  document.getElementById('out').value = decbin(e.value,8);
}

      </script>  
   </body>
</html>

Related Tutorials