# Explain about bitwise operators in JavaScript?

JavascriptObject Oriented ProgrammingProgramming

Bitwise operators are AND,OR,XOR.Let's discuss them individually.

a)  AND operator

Live Demo

### Example

<html>
<body>
<p id="and"></p>
<script>
document.getElementById("and").innerHTML = 13 & 1;
</script>
</body>
</html>

### Output

1

Explanation: Bitwise AND gives a value 1 when there are 2 ones in the same position.In the above example 13 in binary 1101 and 1 in binary 0001.So comparing both we have only one 1 common at the 1st position.So the value is 0001 that is 1.

b)  OR operator

Live Demo

### Example

<html>
<body>
<p id="or"></p>
<script>
document.getElementById("or").innerHTML = 5 || 1;
</script>
</body>
</html>

### Output

5

Explanation: Bitwise OR gives 1 when one of the digits is 1.In the above example the bitwise notation for 5 is 0101 and 1 is 0001 so the value will be 0101 that is 5.

c) XOR operator

Live Demo

### Example

<html>
<body>
<p id="xor"></p>
<script>
document.getElementById("xor").innerHTML = 5 ^ 1;
</script>
</body>
</html>

### Output

4

Explanation: Bitwise XOR gives 1 when the bits are different if not it gives 0. In the above example 5 bitwise notation is 0101 and 1 is 0001. so the result is 0100 that is 4.

Published on 22-May-2019 11:37:53