Page 198 - Web_Application_v2.0_C12_Fb
P. 198
Bitwise Operators
Bitwise operators deal with bits and perform operations on them one by one. The following table shows the
bitwise operators provided by JavaScript:
Operator Name Description Example
& Bitwise AND If both bits are 1, sets each bit to 1. x & y
| Bitwise OR If one of two bits is 1, sets each bit to 1. x | y
^ Bitwise XOR If just one of two bits is 1, sets each bit to 1. x ^ y
~ Bitwise NOT All bits are inverted x ~ y
<< Left shift Shifts left by bringing zeros in from the right and dropping the leftmost x << y
bits.
>> Right shift Allows the rightmost bits to fall off and copies of the leftmost bit to be x >> y
pushed in from the left.
>>> Zero fill right shift By pushing zeros in from the left, it shifts right and lets the rightmost x >>> y
pieces fall off.
Example 9: To demonstrate the use of bitwise operators
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE> JavaScript Bitwise Operators </TITLE>
</HEAD>
<BODY>
<H1> JavaScript Bitwise Operators </H1>
<SCRIPT>
var a = 9, b = 65;
document.write("Bitwise AND operator a & b = " + (a&b));
document.write("<br> Bitwise OR operator a | b = " + (a|b));
document.write("<br> Bitwise EXCLUSIVE OR operator a^b = " + (a^b));
document.write("<br> Bitwise NOT operator ~a = " + (~a));
document.write("<br> LEFT SHIFT operator a << 1 = " + (a << 1));
document.write("<br> RIGHT SHIFT operator b >> 1 = " + (b >> 1));
</SCRIPT>
</BODY>
</HTML>
Output:
196 Touchpad Web Applications (Ver. 2.0)-XII

