The Wayback Machine - https://web.archive.org/web/20240917185402/https://www.geeksforgeeks.org/xor-bitwise-operator-in-javascript/
Open In App

XOR(^) Bitwise Operator in JavaScript

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

In JavaScript, the bitwise XOR(^) Operator is used to compare two operands and return a new binary number which is 1 if both the bits in operators are different and 0 if both the bits in operands are the same. The operation is represented by the “^” symbol. This operator can be used to find the missing numbers in an array of natural numbers.

Let’s look at the truth table below to better understand the output of the XOR operation between two bits.

ABOUTPUT ( A ^ B )
000
011
101
110

Syntax: 

a ^ b

Example 1: In this example, we will perform the XOR operation on a Number.

JavaScript
let a = 5;
let b = 4;
console.log(a^b);

Output:

1

Example 2: In this example, we will use the XOR operator to find missing the missing natural number in an array.

JavaScript
function getMissingNo(a, n)
{
    let x1 = a[0];
    let x2 = 1;
    for (let i = 1; i < n; i++) x1 = x1 ^ a[i];
    for (let i = 2; i <= n + 1; i++) x2 = x2 ^ i;

    return x1 ^ x2;
}
let arr = [1, 2, 3, 5];
let N = arr.length;
let missingNo = getMissingNo(arr, N);
console.log(missingNo);

Output: When we XOR two equal numbers they cancel each other. Here we find the XOR of all elements up to a length of the array and then XOR the natural numbers up to that length. When we XOR both the result together they do not cancel as one number is missing. The missing number is returned after the XOR operation.

4

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

We have a complete list of JavaScript Bitwise Operators, to check those please go through, the JavaScript Bitwise Operators article


Previous Article
Next Article

Similar Reads

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
Bitwise AND Assignment (&=) Operator in JavaScript
The Bitwise AND Assignment Operator is represented by "&amp;=". 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(&amp;) 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(&amp;) 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 "&amp;" 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(&gt;&gt;&gt;) 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 "&lt;&lt;" 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 OR Assignment (|=) Operator in JavaScript
The Bitwise OR Assignment Operator in JavaScript is represented by (|=). This operator is used to perform a bitwise OR operation on both operands and assign the result to the left operands. Syntax: a |= b Where - a = First operandb = Second operand Example 1: In this example, we will use basic numeric values to perform Bitwise OR Assignment Operati
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
Why we should prefer square operator over the dot operator to access the value from the object ?
The square notation should be used as much as possible as compared to the dot notation to access the value from the object in JavaScript. Let's first understand how to obtain the value by using the square notation and dot notation from the object. Square notation: In the square notation the keyName that is passed is treated as a variable and search
3 min read
Length of the longest subarray whose Bitwise XOR is K
Given an array arr[] of size N and an integer K, the task is to find the length of the longest subarray having Bitwise XOR of all its elements equal to K. Examples: Input: arr[] = { 1, 2, 4, 7, 2 }, K = 1Output: 3Explanation: Subarray having Bitwise XOR equal to K(= 1) are { { 1 }, { 2, 4, 7 }, { 1 } }.Therefore, the length of longest subarray havi
11 min read
JavaScript Course Conditional Operator in JavaScript
JavaScript Conditional Operators allow us to perform different types of actions according to different conditions. We make use of the 'if' statement. if(expression){ do this; } The above argument named 'expression' is basically a condition that we pass into the 'if' and if it returns 'true' then the code block inside it will be executed otherwise n
3 min read
Left Shift Assignment (&lt;&lt;=) Operator in JavaScript
The Left Shift Assignment Operator is represented by "&lt;&lt;=". This operator moves the specified number of bits to the left and assigns that result to the variable. We can fill the vacated place by 0. The left shift operator treats the integer stored in the variable to the operator's left as a 32-bit binary number. This can also be explained as
2 min read
JavaScript ‘===’ vs ‘==’Comparison Operator
In Javascript(ES6), there are four ways to test equality which are listed below: Using '==' operatorUsing '===' operatorSameValueZero: used mainly in sets, maps and arrays.SameValue: used elsewhereNow, our main concern is getting to know the difference between the '==' and '===' operators that the javascript provides, though they look similar, they
3 min read
Arrow operator in ES6 of JavaScript
ES6 has come with various advantages and one of them is the arrow operator. It has reduced the function defining code size so it is one of the trending questions asked in the interview. Let us have a deeper dive into the arrow operator's functioning. Syntax: In ES5 a function is defined by the following syntax: function functionName(arg1, arg2….) {
4 min read
The 'new' operator in Javascript for Error Handling
In this article, we will see new operator usage during error throwing as well as handling the error() method in JavaScript using theoretical as well as coding examples. JavaScript new operator: The new operator is actually a special operator that is used along with the error() method which is the method for the error class used to instantiate any s
3 min read
What is the !! (not not) operator in JavaScript?
The !!(not not) is the repetition of the unary logical operator not(!) twice. The double negation(!!) operator calculates the truth value of a value. This operator returns a boolean value, which depends on the truthiness of the given expression. In general, logical not(!) determines the "truth" of what a value is not: The truth is that false is not
2 min read
How to get negative result using modulo operator in JavaScript ?
The %(modulo) operator in JavaScript gives the remainder obtained by dividing two numbers. There is a difference between the %(modulo) and the remainder operator. When remainder or %(modulo) is calculated on positive numbers then both behave similarly but when negative numbers are used then both behave differently. The JavaScript %(modulo) behaves
4 min read
What is JavaScript &gt;&gt;&gt; Operator and how to use it ?
The JavaScript &gt;&gt;&gt; represents the zero-fill right shift operator. It is also called the unsigned right-bit shift operator. It comes under the category of Bitwise operators. Bitwise operators treat operands as 32-bit integer numbers and operate on their binary representation. Zero-fill right shift (&gt;&gt;&gt;) operator: It is a binary ope
3 min read
What does +_ operator mean in JavaScript ?
Unary Operator: A unary operation contain only one operand. Here, the '+' unary plus operator converts its operand to Number type. While it also acts as an arithmetic operator with two operands which returns an addition result on calculation. JavaScript Identifiers: Javascript Identifiers are used to name variables (and keywords, functions, and lab
2 min read
JavaScript Unsigned Right Shift Assignment Operator
In JavaScript "&gt;&gt;&gt;=" is known as the unsigned right shift assignment bitwise operator. This operator is used to move a particular amount of bits to the right and returns a number that is assigned to a variable. Syntax: a &gt;&gt;&gt;= b Meaning: a = a &gt;&gt;&gt; b Return value: It returns the number after shifting of bits. Example 1: Thi
1 min read
JavaScript SyntaxError - Missing name after . operator
This JavaScript exception missing name after . operator occurs if the dot operator (.) is used in the wrong manner for property access. Message: SyntaxError: missing name after . operator Error Type: SyntaxError Cause of Error: The dot operator (.) is used to access the property. Users will have to provide the name of the properties to access. Some
1 min read
JavaScript SyntaxError - Applying the 'delete' operator to an unqualified name is deprecated
This JavaScript exception applying the 'delete' operator to an unqualified name is deprecated works in strict mode and it occurs if variables are tried to be deleted with the delete operator. Message: SyntaxError: Calling delete on expression not allowed in strict mode (Edge) SyntaxError: applying the 'delete' operator to an unqualified name is dep
1 min read
JavaScript TypeError - Cannot use 'in' operator to search for 'X' in 'Y'
This JavaScript exception Cannot use 'in' operator to search for 'X' in 'Y' occurs if in operator is used to search in strings, numbers, or other primitive types. It can not be used other than type-checking. Message: TypeError: Invalid operand to 'in' (Edge) TypeError: right-hand side of 'in' must be an object, got 'x' (Firefox) TypeError: cannot u
1 min read
JavaScript Remainder(%) Operator
The remainder operator in JavaScript is used to get the remaining value when an operand is divided by another operand. In some languages, % is considered modulo. Modulo and Remainder work differently when the sign of both operands is different. In JavaScript remainder takes the sign of the dividend and to get modulo ((a % n) + n) % n should be used
2 min read
Nullish Coalescing Assignment (??=) Operator in JavaScript
This is a new operator introduced by javascript. This operator is represented by x ??= y and it is called Logical nullish assignment operator. Only if the value of x is nullish then the value of y will be assigned to x that means if the value of x is null or undefined then the value of y will be assigned to x. Let's discuss how this logical nullish
2 min read
JavaScript Logical AND assignment (&amp;&amp;=) Operator
This operator is represented by x &amp;&amp;= y, and it is called the logical AND assignment operator. It assigns the value of y into x only if x is a truthy value. We use this operator x &amp;&amp;= y like this. Now break this expression into two parts, x &amp;&amp; (x = y). If the value of x is true, then the statement (x = y) executes, and the v
2 min read
JavaScript Logical OR assignment (||=) Operator
This operator is represented by x ||= y and it is called a logical OR assignment operator. If the value of x is falsy then the value of y will be assigned to x. When we divide it into two parts it becomes x || ( x = y ). It checks if x is true or false, if the value of x is falsy then it runs the ( x = y ) block and the value of y gets stored into
2 min read
JavaScript Arithmetic Unary Plus(+) Operator
The Unary plus(+) operation is a single operand operator (which means it worked with only a single operand preceding or succeeding to it), which is used to convert its operand to a number, if it isn't already a number. Syntax: +Operand Below examples illustrate the Unary plus(+) Operator in JavaScript: Example 1:This example shows the use of the Ja
1 min read
Article Tags :