Advertisement
cortez

JavaScript-Exp-Ops Ex. 11

Oct 12th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/Javascript">
  2.         function extractBit() {
  3.  
  4.     var n = document.getElementById("extractBit").value;
  5.     var p= document.getElementById("extractPos").value;
  6.     var mask = 1 << p; // moving the first bit to the left by bPos positions.
  7.  
  8.     var result= (n & mask) != 0 ? 1 : 0; // we are using the & operator to see if the bit is 1
  9.  
  10.     alert("The " + p+ " bit of the number " + n + " is: " + result)
  11.  
  12.         }
  13. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement