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
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

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.