»
EnglishFrenchVietnamese

Print - Bitwise operators - JavaScriptBank.com

Full version: jsB@nk » Calculation » Bitwise operators
URL: https://www.javascriptbank.com/bitwise.html

Bitwise operators © JavaScriptBank.comThis JavaScript can be used to calculate some bitwise operators.

Full version: jsB@nk » Calculation » Bitwise operators
URL: https://www.javascriptbank.com/bitwise.html



JavaScript
<script language="JavaScript1.2"><!--  //Convert Decimal to Binary  function toBinStr(decval) {    nbb=32; //32bit integers    tmpChar="";    //Check if negative value    neg = false;    if (decval<0) {      decval = -decval;      neg = true;    }    //Binary Code of the positive value    for(i=nbb-1;i>=0;i--) {      if ((decval / Math.pow(2,i)) >= 1) {         tmpChar = tmpChar + "1";         decval = decval % Math.pow(2,i);      } else         tmpChar = tmpChar + "0";    }    if (neg) {      //Complement Code      tmp2="";      for (i=0;i<nbb;i++) {        if (tmpChar.charAt(i)=="0") {            tmp2 += "1" ;        } else {            tmp2 += "0";        }      }      tmpChar = tmp2;      //Two's Complement Code      tmp2="";      ovf = 1;      i   =nbb-1;      while ((ovf==1)&&(i>=0)) {        b = 1 ^ tmpChar.charAt(i);        ovf = tmpChar.charAt(i);        tmp2 = b.toString() + tmp2;        i--;      }      for (;i>=0;i--) {        tmp2 = tmpChar.charAt(i) + tmp2;      }      tmpChar = tmp2;    }    return tmpChar;  }   function TestIt(fm, operation) {          o1 = fm.op1.value;          o2 = fm.op2.value;      //If EmptyFields||NonDigitFields { ErrorMsg("err") }      reNonDigital = /[^\d\+-]/;      if ((fm.op1.value=="")||(reNonDigital.exec(fm.op1.value)!=null)) {         alert("Enter decimal value!");         fm.op1.focus();         return false;      }      if ((fm.op2.value=="")||(reNonDigital.exec(fm.op2.value)!=null)) {         alert("Enter decimal value!");         fm.op2.focus();         return false;      }      //Calculate Result      switch (operation) {         case 6:            fm.rs6.value = ~ fm.op1.value;            fm.descr.value = " ~ " + toBinStr(o1) + " = " + toBinStr(fm.rs6.value);            break;         case 5:            fm.rs5.value = fm.op1.value ^ fm.op2.value;            fm.descr.value = toBinStr(o1) + " ^ " + toBinStr(o2) + " = " + toBinStr(fm.rs5.value);            break;         case 4:            fm.rs4.value = fm.op1.value | fm.op2.value;            fm.descr.value = toBinStr(o1) + " | " + toBinStr(o2) + " = " + toBinStr(fm.rs4.value);            break;         case 3:            fm.rs3.value = fm.op1.value & fm.op2.value;            fm.descr.value = toBinStr(o1) + " & " + toBinStr(o2) + " = " + toBinStr(fm.rs3.value);            break;         case 2:            fm.rs2.value = fm.op1.value << fm.op2.value;            fm.descr.value = toBinStr(o1) + " << " + o2 + " = " + toBinStr(fm.rs2.value);            break;         case 1:            fm.rs1.value = fm.op1.value >>> fm.op2.value;            fm.descr.value = toBinStr(o1) + " >>> " + o2 + " = " + toBinStr(fm.rs1.value);            break;         case 0:         default:            fm.rs0.value = fm.op1.value >> fm.op2.value;            fm.descr.value = toBinStr(o1) + " >> " + o2 + " = " + toBinStr(fm.rs0.value);      }      //Display Binary Operation      return true;   } // --> </script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<form> <table align="center" border="1">  <tbody><tr>   <th colspan="4" align="center">BITWISE OPERATORS</th>  </tr>  <tr>   <td rowspan="7" align="center" valign="top">   <input type="text" name="op1" value="-9" size="20"></td>   <td align="center"><input type="button" name="shftRS" value=" &gt;&gt; " onclick="TestIt(this.form, 0)"></td>   <td rowspan="6" align="center" valign="top">   <input type="text" name="op2" value="1" size="20">&nbsp;&nbsp;=&nbsp;</td>   <td align="center"><input type="text" name="rs0" value="" size="20"></td>  </tr>  <tr>   <td align="center"><input type="button" name="shftR" value="&gt;&gt;&gt;" onclick="TestIt(this.form, 1)"></td>   <td align="center"><input type="text" name="rs1" value="" size="20"></td>  </tr>  <tr>   <td align="center"><input type="button" name="shftL" value=" &lt;&lt; " onclick="TestIt(this.form, 2)"></td>   <td align="center"><input type="text" name="rs2" value="" size="20"></td>  </tr>  <tr>   <td align="center"><input type="button" name="bwAND" value="  &amp;  " onclick="TestIt(this.form, 3)"></td>   <td align="center"><input type="text" name="rs3" value="" size="20"></td>  </tr>  <tr>   <td align="center"><input type="button" name="bwOR" value="  |  " onclick="TestIt(this.form, 4)"></td>   <td align="center"><input type="text" name="rs4" value="" size="20"></td>  </tr>  <tr>   <td align="center"><input type="button" name="bwXOR" value="  ^  " onclick="TestIt(this.form, 5)"></td>   <td align="center"><input type="text" name="rs5" value="" size="20"></td>  </tr>  <tr>   <td align="center"><input type="button" name="bwNOT" value="  ~  " onclick="TestIt(this.form, 6)"></td>   <td align="center">--</td>   <td align="center"><input type="text" name="rs6" value="" size="20"></td>  </tr>  <tr>   <td colspan="4" align="center"><input type="text" name="descr" size="105" value=""></td>  </tr>  <tr>   <td colspan="4" align="center"><input type="reset" name="reset" value="Reset"></td>  </tr> </tbody></table></form><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->