JavaScript Bitwise Operators
You are Here:
JavaScript Bitwise Operators
The bitwise operators
work by converting values (num1 and num2) to 32-bit binary numbers and then comparing the individual bits of these two binary numbers. The result is returned as a normal decimal number.
For example, the decimal number 52 has a binary representation of 110100. Please check our decimal to binary Online converter for reference.
AND Operator
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var num1 = 4;
var num2 = 1;
document.write(num1 & num2);
</script>
</body>
</html>
JavaScript Bitwise Operators
Operator | Explanation | Operation | Result |
---|---|---|---|
& | AND | 4 & 1 | 0 |
| | OR | 4 | 1 | 5 |
^ | XOR | 4 ^ 1 | 5 |
~ | NOT | ~4 | -5 |
<< | Zero fill left shift | 4 << 1 | 8 |
>> | Signed right shift | 4 >> 1 | 2 |
>>> | Zero fill right shift | 4 >>> 1 | 2 |
OR Operator
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var num1 = 4;
var num2 = 1;
document.write(num1 | num2);
</script>
</body>
</html>
XOR Operator
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var num1 = 4;
var num2 = 1;
document.write(num1 ^ num2);
</script>
</body>
</html>
NOT Operator
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var num = 4;
document.write(~num);
</script>
</body>
</html>
Zero Fill Left Shift Operator
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var num1 = 4;
var num2 = 1;
document.write(num1 << num2);
</script>
</body>
</html>
Signed Right Shift Operator
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var num1 = 4;
var num2 = 1;
document.write(num1 >> num2);
</script>
</body>
</html>
Zero Fill Right Shift Operator
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var num1 = 4;
var num2 = 1;
document.write(num1 >>> num2);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.