What are Bitwise Operators?

What are Bitwise Operators?

Ferenc Almasi β€’ 2021 January 15 β€’ Read time 1 min read
  • twitter
  • facebook
JavaScript

Bitwise operators treat their operands as bits: zeroes and ones. There are three bitwise operators that you need to know about:

  • Bitwise AND: &
  • Bitwise OR: |
  • Bitwise NOT: ~

Each of them used for different purposes.

The bitwise AND operator

The Bitwise AND operator returns 1 in each bit position for which the corresponding bits of both operands are 1. For example:

Copied to clipboard!
0 & 0 // returns 0
0 & 1 // returns 0
1 & 0 // returns 0
1 & 1 // returns 1
bitwise-and.js

The bitwise OR operator

Unlike the AND operator, the bitwise OR operator returns 1 in each bit position for which the corresponding bits of either one of the operands are 1. For example:

Copied to clipboard!
0 | 0 // returns 0
0 | 1 // returns 1
1 | 0 // returns 1
1 | 1 // returns 1 
bitwise-or.js

The bitwise NOT operator

The bitwise NOT operator is used to invert the bits of its operand. For example:

Copied to clipboard!
~0  // returns -1
~-1 // returns 0
~1  // returns -2
~-2 // returns 1
bitwise-not.js
What are the purpose of bitwise operators in JavaScript?
If you would like to see more Webtips, follow @flowforfrank

50 JavaScript Interview Questions

Resources:

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.