The Wayback Machine - https://web.archive.org/web/20240930180839/https://www.geeksforgeeks.org/javascript-bitwise-operators/
Open In App

JavaScript Bitwise Operators

Last Updated : 02 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript uses 32-bit Bitwise operands. A number is stored as a 64-bit floating-point number but the bit-wise operation is performed on a 32-bit binary number i.e. to perform a bit-operation JavaScript converts the number into a 32-bit binary number (signed) and performs the operation and converts back the result to a 64-bit number.

Bitwise Operators List

OPERATOR NAMEUSAGEDESCRIPTION
Bitwise AND(&)a & bReturns true if both operands are true
Bitwise OR(|)a | bReturns true even if one operand is true
Biwise XOR(^)a ^ bReturns true if both operands are different
Bitwise NOT(~)~ aFlips the value of the operand
Bitwise Left Shift(<<)a << bShifts the bit toward the left
Bitwise Right Shift(>>)a >> bShifts the bit towards the right
Zero Fill Right Shift(>>>)a >>> bShifts the bit towards the right but adds 0 from left

List of Bitwise Operators with Explanation

1. Bitwise AND Operator ( & )

It is a binary operator i.e. accepts two operands. Bit-wise AND (&) returns 1 if both the bits are set ( i.e 1) and 0 in any other case.

ABOUTPUT ( A & B )
000
010
100
111

2. Bitwise OR Operator ( | )

It is a binary operator i.e. accepts two operands. Bit-wise OR ( | ) returns 1 if any of the operands is set (i.e. 1) and 0 in any other case.

ABOUTPUT ( A | B )
000
011
101
111

3. Bitwise XOR Operator ( ^ )

It is a binary operator i.e. accepts two operands. Bit-wise XOR ( ^ ) returns 1 if both the operands are different and 0 in any other case.

ABOUTPUT ( A ^ B )
000
011
101
110

4. Bitwise NOT Operator ( ~ )

It is a unary operator i.e. accepts single operands. Bit-wise NOT ( ~ ) flips the bits i.e 0 becomes 1 and 1 becomes 0.

AOUTPUT ( ~A )
01
10

Below is an example of the JavaScript Bitwise Operators:

Example: 

JavaScript
let a = 4; 
let b = 1; 

console.log("A & B = " + (a & b)); 
console.log("A | B = " + (a | b)); 
console.log("~A = " + (~a));

Output
A & B = 0
A | B = 5
~A = -5

5. Left Shift Operator ( << )

It’s a binary operator i.e. it accepts two operands. The first operator specifies the number and the second operator specifies the number of bits to shift. Each bit is shifted towards the left and 0 bits are added from the right. The excess bits from the left are discarded.

A6 ( 00000000000000000000000000000110 )
B1 ( 00000000000000000000000000000001 )
OUTPUT ( A << B )12 ( 00000000000000000000000000001100 )

6. Sign Propagating Right Shift Operator ( >> )

It’s a binary operator i.e. it accepts two operands. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bit is shifted towards the right, the overflowing bits are discarded. This is Sign Propagating as the bits are added from the left depending upon the sign of the number (i.e. 0 if positive and 1 if negative )

A6 ( 00000000000000000000000000000110 )
B1 ( 00000000000000000000000000000001 )
OUTPUT ( A >> B )3 ( 00000000000000000000000000000011 )

7. Zero Fill Right Shift Operator ( >>> )

It’s a binary operator i.e. it accepts two operands. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bit is shifted towards the right, the overflowing bits are discarded. 0 bit is added from the left so its zero fill right shift.

A6 ( 00000000000000000000000000000110 )
B1 ( 00000000000000000000000000000001 )
OUTPUT ( A >>> B )3 ( 00000000000000000000000000000011 )

Example: Below is the implementation of the above-described operators.

JavaScript
let a = 6;
let b = 1;

// AND Operation
console.log("A & B = " + (a & b));

// OR operation
console.log("A | B = " + (a | b));

// NOT operation
console.log("~A = " + (~a));

// Sign Propagating Right Shift
console.log("A >> B = " + (a >> b));

// Zero Fill Right Shift
console.log("A >>> B = " + (a >>> b));

// Left Shift
console.log("A << B = " + (a << b)); 

Output:

A & B = 0
A | B = 7
~A = -7
A >> B = 3
A >>> B = 3
A << B = 12

Supported Browsers: The browsers supported by all JavaScript Bitwise operators are listed below.

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera

We have a complete list of Javascript operators, to check those please go through this Javascript Operators Complete Reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

JavaScript Bitwise Operators – FAQs

How does the & (AND) operator work?

The & operator performs a bitwise AND operation. It returns a binary number where each bit is 1 if the corresponding bits of both operands are 1, and 0 otherwise.

How does the | (OR) operator work?

The | operator performs a bitwise OR operation. It returns a binary number where each bit is 1 if at least one of the corresponding bits of the operands is 1.

How does the ^ (XOR) operator work?

The ^ operator performs a bitwise XOR (exclusive OR) operation. It returns a binary number where each bit is 1 if the corresponding bits of the operands are different, and 0 if they are the same.

How does the ~ (NOT) operator work?

The ~ operator performs a bitwise NOT operation. It inverts each bit of its operand, turning 0s into 1s and 1s into 0s.

How does the << (Left Shift) operator work?

The << operator shifts the bits of its left operand to the left by the number of positions specified by its right operand. Bits shifted out on the left are discarded, and zeros are shifted in from the right.

How does the >> (Sign-propagating Right Shift) operator work?

The >> operator shifts the bits of its left operand to the right by the number of positions specified by its right operand. Bits shifted out on the right are discarded. The leftmost bit (sign bit) is copied to fill the leftmost positions, preserving the sign of the number.

How does the >>> (Zero-fill Right Shift) operator work?

The >>> operator shifts the bits of its left operand to the right by the number of positions specified by its right operand. Bits shifted out on the right are discarded. Zeros are shifted in from the left, regardless of the sign of the original number.

What are some common uses of bitwise operators?

Bitwise operators are often used in low-level programming, such as:

  • Setting, clearing, and toggling individual bits in a number.
  • Performing bitwise manipulations for graphics and cryptography.
  • Efficiently encoding and decoding data.
  • Implementing bit masks to select specific bits from a binary value.


Previous Article
Next Article

Similar Reads

PHP | Bitwise Operators
The Bitwise operators is used to perform bit-level operations on the operands. The operators are first converted to bit-level and then calculation is performed on the operands. The mathematical operations such as addition , subtraction , multiplication etc. can be performed at bit-level for faster processing. In PHP, the operators that works at bit
5 min read
JavaScript Course Logical Operators in JavaScript
logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. There are three types of logical operators in Javascript: !(NOT): Converts operator to boolean and returns flipped value &&:(AND) Evaluates operands and retu
3 min read
JavaScript Course Operators in JavaScript
An operator is capable of manipulating a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used for comparing values, performing arithmetic operations, etc. There are various operators
7 min read
Alternative to Multiple OR Operators in JavaScript
In this article, we will learn the alternatives to the use of multiple || (OR operator) in conditional statements. Multiple OR operators in conditional statements might cause poor readability of code and are often limited in functionality. Example: The below JavaScript code could be rewritten to perform the same task in other ways: C/C++ Code let u
4 min read
What are Comparison Operators in JavaScript ?
Comparison operators in JavaScript are used to compare two values and return a boolean result (true or false). They include operators such as ==, ===, !=, !==, >, <, >=, and <=. Example: Here, the > (greater than) the operator compares the values of a and b. Since a is not greater than b, the expression a > b evaluates to false, w
1 min read
Does JavaScript Operators like or ( || ), and ( && ), null coalescing ( ?? ) makes Weak Comparison?
JavaScript operators like logical OR (||), logical AND (&&), and null coalescing (??) are used for making conditional evaluations and performing logical operations in JavaScript. While these operators are commonly used for controlling the flow of the code execution and handling values in the different scenarios it's essential to understand
2 min read
JavaScript Operators Reference
JavaScript Operators are used to perform specific mathematical and logical computations on operands. In other words, an operator operates the operands. In JavaScript, operators are used to compare values, perform arithmetic operations, etc. Example: In this example, we will use typeof operator to check the type of variable. [GFGTABS] JavaScript let
5 min read
JavaScript Operators
JavaScript Operators are symbols used to perform specific mathematical, comparison, assignment, and logical computations on operands. They are fundamental elements in JavaScript programming, allowing developers to manipulate data and control program flow efficiently. Understanding the different types of operators and how they work is important for
11 min read
Difference Between && and || Operators in javaScript
In JavaScript, logical operators && (AND) and || (OR) are used to perform the logical operations on values. These operators are commonly used in the control flow and decision-making within the programs. Understanding the differences between these operators is essential for writing efficient and accurate code. These are the following topics
3 min read
JavaScript Comparison Operators
JavaScript comparison operators are essential tools for checking conditions and making decisions in your code. They are used to evaluate whether a condition is true or false by comparing variables or values. These operators play a crucial role in logical expressions, helping to determine the equality or differences between values. In this article,
6 min read
JavaScript Logical Operators
Logical operators in JavaScript are used for making decisions based on conditions and manipulating boolean values. They are commonly used to compare variables or values and set termination conditions for loops. In JavaScript, there are basically three types of logical operators. OPERATOR NAMEOPERATOR SYMBOLOPERATIONNOT!Converts operator to boolean
4 min read
JavaScript Unary Operators
JavaScript Unary Operators work on a single operand and perform various operations, like incrementing/decrementing, evaluating data type, negation of a value, etc. Table of Content Unary Plus (+) OperatorUnary Minus (-) OperatorUnary Increment (++) OperatorUnary Decrement (--) OperatorLogical NOT (!) OperatorBitwise NOT (~) Operatortypeof Operatord
6 min read
JavaScript Relational operators
JavaScript Relational Operators are used to compare their operands and determine the relationship between them. They return a Boolean value (true or false) based on the comparison result. Types of Relational OperatorsThese are two types of relational operators, these are: JavaScript in OperatorJavaScript instanceof OperatorWe will explore all the a
3 min read
JavaScript String Operators
JavaScript String Operators are used to manipulate and perform operations on strings. There are two operators which are used to modify strings in JavaScript. These operators help us to join one string to another string. Type of JavaScript String OperatorsThere are two type of String Operators in JavaScript, these are: String Concatenate OperatorStr
2 min read
JavaScript Assignment Operators
Assignment Operators Assignment operators are used to assign values to variables in JavaScript. Syntax:data=valueExample: // Lets take some variablesx=10y=20x=y // Here, x is equal to 20y=x // Here, y is equal to 10Assignment Operators ListThere are so many assignment operators as shown in the table with the description. OPERATOR NAMESHORTHAND OPER
6 min read
How to use multiple ternary operators in a single statement in JavaScript ?
In JavaScript, the ternary operator (? :) is a shorthand for if-else statements. You can use multiple ternary operators in a single statement to handle multiple conditions efficiently. This makes your code shorter, but be cautious as too many nested ternary operators can reduce code readability. Syntax:condition1 ?(condition2 ? Expression1 : Expres
2 min read
JavaScript Arithmetic Operators
JavaScript Arithmetic Operators are the operator that operate upon the numerical values and return a numerical value. Arithmetic Operators listThere are many arithmetic operators as shown in the table with the description. OPERATOR NAME USAGE OPERATION Addition Operatora + bAdd two numbers or concatenate the stringSubtraction Operatora - bDifferenc
7 min read
Javascript Short Circuiting Operators
In JavaScript, short-circuiting refers to the process of evaluating expressions from left to right and stopping as soon as the result is determined. If the outcome of an expression is clear before evaluating all conditions, the evaluation is “short-circuited,” meaning the remaining conditions are ignored. This leads to more efficient processing by
2 min read
Javascript Program to Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
Given an array A[] of length N, where N is an even number, the task is to answer Q independent queries where each query consists of a positive integer K representing the number of circular shifts performed on the array and find the sum of elements by performing Bitwise OR operation on the divided array.Note: Each query begins with the original arra
5 min read
Bitwise AND Assignment (&=) Operator in JavaScript
The Bitwise AND Assignment Operator is represented by "&=". This operator uses the binary representation of both operands and performs the bitwise AND operation and then assigns the result to the left operand. This can also be explained as applying the logical AND operation to the first operand and second operand and after that assigning the re
1 min read
AND(&) Bitwise Operator in JavaScript
JavaScript Bitwise AND(&) operator is used to compare two operands by performing an AND operation on the individual bits and returning 1 only if both the bits are one. The AND(&) Operator has a lot of real-world applications and the most famous one is to check whether a number is even or odd. The operation is represented by the "&" symb
2 min read
OR(|) Bitwise Operator in JavaScript
JavaScript Bitwise OR(|) Operator is used to compare two operands by performing OR operation on individual bits of the operands and returns true even if one of the compared bits is 1. The OR Operator has vast applications and the most used one is combining bit values. The operation is represented by the "|" symbol. Let's look at the truth table bel
2 min read
NOT(~) Bitwise Operator in JavaScript
JavaScript NOT(~) Operator is used to invert the bits of a number. The operator is represented by "~" symbol. It is a unary operator since it requires only one operand to operate. There are various uses of the Bitwise NOT operator which include bit-masking, finding two's complement as well as error correction. Let's look at the truth table below to
1 min read
Zero Fill Right Shift (>>>) Bitwise Operator in JavaScript
JavaScript Zero Fill Right Shift Operator or Unsigned Right Shift Operator(>>>) is used for operating on the two operands. The first operand is the number and the right operand specifies the bits to shift towards the right modulo 32. In this way, the excess bits which are shifted towards the right are discarded and zero bits are added towa
2 min read
Right Shift (>>) Bitwise Operator in JavaScript
JavaScript bitwise right shift operator is used to operate on two operands where the left operand is the number and the right operand specifies the number of bits to shift towards the right. A copy of old leftmost bits is maintained and they have added again the shifting is performed. The sign bit is not changed so it is also called sign propagatin
2 min read
Left Shift (<<) Bitwise Operator in JavaScript
JavaScript Left Shift Operator is used to operate on the two operands. The first operand is the number and the right operand specifies the number of bits to shift to the left. The operation is represented by the "<<" symbol. Mainly the left shift operator is used to multiply the number by any power of 2. Let's look at the table below to bette
2 min read
Bitwise XOR Assignment (^=) Operator in JavaScript
The Javascript Bitwise XOR assignment is represented by (^=). It is used to perform a bitwise XOR operation on both operands and assign the result to the left operand. Syntax: a ^= b // a = a ^ b Where - a = First operandb = Second operand Example: In this example, we will perform the basic Bitwise XOR assignment operation on both operands. C/C++ C
2 min read
Compute the Bitwise XOR of all Numbers in a Range using JavaScript
The complexity of bitwise XOR computation for all the numbers in a specific range is the most frequent difficulty that many people face during the problem-solving process. Problem Description: Given two integers, L and R, find the bitwise XOR of all numbers in the inclusive range from L to R. There are several approaches to computing the bitwise XO
6 min read
JavaScript Program to Compute the Bitwise OR of all Numbers in a Range
In JavaScript, bitwise operations provide a way to manipulate individual bits of numeric values. The bitwise OR operation (|) combines the bits of two operands, resulting in a new value where each bit is set if it is set in either operand. Understanding Bitwise ORThe bitwise OR operation takes two operands and performs a logical OR operation on eac
2 min read
Compute the Bitwise AND of all Numbers in a Range using JavaScript
The complexity of bitwise AND computation for all numbers in a specific range is a common challenge faced by developers, particularly when optimizing algorithms. We have given two integers, L and R, and find the bitwise AND of all numbers in the inclusive range from L to R in JavaScript. Example: Let's consider an example where L=3 and R=6.The bitw
3 min read