Bitwise operators work with binary data at the bit level in JavaScript. They process numbers in binary form and return a new number after applying a specific bit rule.
Table of Content
What are Bitwise Operators in JavaScript
Bitwise operators are symbols that act on the binary form of numbers. JavaScript changes each number into a 32-bit integer and runs the operation on each bit.
When JavaScript runs a bitwise operation, it first changes the number to binary. It compares bits from two numbers or flips bits from one number. It then changes the result back to decimal.
Here is a quick example:
let a = 5; // binary: 0101
let b = 3; // binary: 0011
let result = a & b; // binary: 0001 => decimal: 1
console.log(result);In this example, the & operator compares each bit. If both bits are 1, the result bit is 1. Only the last bit matches, so the result is 1.
Here are the reasons to use the bitwise operator in JavaScript:
- Work with low-level data control in graphics or encryption.
- Improve speed in some math operations.
- Pack or unpack multiple values inside one number.
Type of Bitwise Operators in JavaScript
Bitwise AND (&) returns 1 in each bit position only when both bits are 1, otherwise 0. Here is an example:
let result = 5 & 3;
console.log(result); // 15in binary:01013in binary:0011- Compare each bit: only the last bit is
1in both →0001(which is1in decimal).
Bitwise OR (|) returns 1 in a bit position when at least one of the bits is 1. For example:
let result = 5 | 3;
console.log(result); // 75→01013→0011- Bit-by-bit:
0111→ decimal7.
Bitwise XOR (^) returns 1 when the bits are different, returns 0 when they are the same. Here is an example:
let result = 5 ^ 3;
console.log(result); // 65→01013→0011- Compare:
0110→ decimal6.
Bitwise NOT (~) flips each bit — 1 becomes 0, and 0 becomes 1. Here is an example:
let result = ~5;
console.log(result); // -65in binary (32-bit signed):00000000000000000000000000000101- Flipped:
11111111111111111111111111111010→ This is-6in two’s complement form.
Bitwise left shift (<<) moves all bits to the left by the given number of positions. Fills the empty right bits with 0.
For example:
let result = 5 << 1;
console.log(result); // 105→0101- Shift left 1 place:
1010→ decimal10. - This is the same as when you multiply by
2once.
Bitwise right shift (>>) moves all bits to the right. It keeps the sign bit (for negative numbers).
For example:
let result = 5 >> 1;
console.log(result); // 25→0101- Shift right 1 place:
0010→ decimal2.
For negative numbers:
console.log(-5 >> 1); // -3The sign bit is preserved, so it rounds toward negative infinity.
Zero-fill right shift (>>>) moves bits to the right and fills the left with 0. It does not keep the sign bit.
let result = -5 >>> 1;
console.log(result); // 2147483645-5in 32-bit binary:11111111111111111111111111111011- Shift right 1, fill left with
0:01111111111111111111111111111101→ decimal2147483645.
The Difference Between Bitwise and Logical Operators
Bitwise operators look at the binary bits of a number and do calculations on each bit. The result is always another number. While the logical operators work with true or false values. They return true or false based on the logic you apply.
Here is a table that shows you the key differences for each one:
| Feature | Bitwise Operator | Logical Operator |
|---|---|---|
| Data type used | Works on numbers | Works on Boolean values |
| Level of operation | Bit level | Logical condition |
| Result type | Number | Boolean |
| Symbol examples | &, |, ^, ~, <<, >>, >>> | &&, ||, ! |
Bitwise operators can be used to check or set bits in binary values. Logical operators are used to control program flow with conditions.
Examples
Mask Bits:
let flags = 13; // binary: 1101
let mask = 4; // binary: 0100
let result = flags & mask;
console.log(result);This example uses & to check if a specific bit is set. It checks if the third bit from the right is on. The output 4 means the bit is on.
Toggle Bits:
let value = 9; // binary: 1001
let toggle = 5; // binary: 0101
let result = value ^ toggle;
console.log(result); // 12The ^ operator flips bits where the mask has 1. This changes the first and third bits in the binary form of the number.
Fast Multiplication by 2:
let num = 7;
let result = num << 1;
console.log(result); //14The << operator moves bits to the left. Each left shift by 1 doubles the number. So 7 becomes 14 after one shift.
Force Unsigned Integer:
let num = -5;
let result = num >>> 0;
console.log(result); // 4294967291This uses >>> to change a signed number into an unsigned 32-bit integer. The result is a large positive number that holds the same bit pattern.
Wrapping Up
In this article, you learned what bitwise operators in JavaScript are and how they work on binary data. You also understood the types of them.
Here is a quick recap:
- Bitwise operators work on the binary bits inside a number.
- You can use them in many tasks, such as:
- Masks
- Toggles
- Shifts
- Low-level data control
FAQs
How do bitwise operators work in JavaScript?
- & - Bitwise AND
- | - Bitwise OR
- ^ - Bitwise XOR
- ~ - Bitwise NOT
let a = 5; // 0101
let b = 3; // 0011
console.log(a & b); // 1 (0001)
What is the difference between bitwise AND and OR in JavaScript?
let x = 6; // 0110
let y = 3; // 0011
console.log(x & y); // 2 (0010)
console.log(x | y); // 7 (0111)
How does the bitwise NOT (~) operator work in JavaScript?
let num = 5; // 00000000000000000000000000000101
console.log(~num); // -6
How to use bitwise shift operators in JavaScript?
- << - Left shift
- >> - Right shift with sign
- >>> - Right shift zero-fill
let n = 2;
console.log(n << 1); // 4 (shift left)
console.log(n >> 1); // 1 (shift right)
console.log(n >>> 1); // 1 (zero-fill right)
Similar Reads
Code mistakes used to slip by without a warning. That made bugs hard to trace and fix. JavaScript "use strict"…
You have to organize your JavaScript code structure. A clean layout helps you read and fix your code. Whether you…
Math.asin() finds the arc sine of a number in JavaScript. It helps you work with angles from values. You get…
JavaScript Math.tan() finds the tangent of an angle in radians. You use it when you need to solve angle-based math.…
Comments in JavaScript help you explain code and prevent mixing. They guide anyone who reads or edits your script. Understand…
The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…
The JavaScript while loop runs code as long as a condition stays true. You can use it to repeat tasks…
AJAX keeps your web app fast. You do not need to reload the page every time you send or get…
Mocha is a JavaScript test framework for Node.js. It runs tests in sequence and shows results. It works with any…
JavaScript gives you the Math.trunc() function to work with decimal numbers. You use it when you want to remove the…