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
JavaScript gives you the Math.round() function to deal with decimal numbers. You use it when you want to round a…
JavaScript uses popup boxes to show quick messages or ask for direct input. These boxes stop the flow of the…
Comments in JavaScript help you explain code and prevent mixing. They guide anyone who reads or edits your script. Understand…
Object constructors and class constructors in JavaScript create objects in a structured way. Both provide methods to build reusable code.…
Arrow function gives you short code and keeps this in the right scope in JavaScript. Understand the Arrow Function in…
The Math object gives you a set of built-in functions and constants for working with numbers in JavaScript. You do…
The ternary operator in JavaScript keeps your code short. It helps you write conditions inside one line. JavaScript lets you…
JavaScript Symbol Type gives a way to make unique values. A Symbol never matches any other value and this helps…
The toString function in JavaScript converts a value to a string form without change. It works on numbers, arrays, and…
JavaScript object methods are simple ways to handle data inside objects. An object can hold many values, and methods give…