Skip to main content

//Bitwise Operator

Bitwise Operator


  1. <!DOCTYPE html>
  2. <html>
  3. <head>

  4. <title></title>
  5. </head>
  6. <body>
  7. <script type="text/javascript">

  8. var a = 50; //00110010 in binary.
  9. var b = 30; //00011110 in binary.
  10. var c = 0;
  11. var d = 0;
  12. var e = 0;
  13. var f = 0;
  14. var g = 0;
  15. var h = 0;

  16. //Bitwise AND
  17. c = a & b; // 00110010 & 00011110 = 00010010 i.e means is '18'.
  18. document.write("the value is: "+c+"<br>"); // <br> use for linebreake

  19. //Bitwise OR
  20. d = a | b; // 00110010 | 00011110 = 00111110 i.e means is '62'.
  21. document.write("the value is: "+d+"<br>"); // document.write() use for print line.

  22. //Bitwise Exclusive XOR
  23. e = a ^ b;  // 00110010 ^ 00011110 = 00101100 i.e means is '44'.
  24. document.write("the value is: "+e+"<br>");// +e+ is used to indicate which value should be print.

  25. //Bitwise Complement
  26. f = ~a; // -(50+1)= i.e means is '-51'.
  27. document.write("the value is: "+f+"<br>");

  28. //Bitwise Shift Left
  29. g = a << 2; //50=110010 << 2 :- 11001000 i.e means is '200'. 
  30.   //Shift with 2 zero to left side.
  31. document.write("the value is: "+g+"<br>");

  32. //Bitwise Shift Right
  33. h = a >> 2; // 50=110010 >> 2 :-  001100 i.e means os '12'. 
  34.   //Shift with 2 zero to right side.
  35. document.write("the value is: "+h+"<br>");
  36. </script>
  37. </body>
  38. </html>



//Execute


//Output

/*

The value of AND is:- 18

The value of OR is:- 62

The value of Exclusive OR is:- 44

The value of Complement is:- -51

The value of Shift Left is:- 200

The value of Shift Right is:- 12

*/






//ThE ProFessoR