- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - Bitwise Operators
JavaScript Bitwise Operators
The bitwise operators in JavaScript perform operations on the integer values at the binary level. They are used to manipulate each bit of the integer values. Bitwise operators are similar to logical operators but they work on individual bits.
JavaScript bitwise operators works on 32-bits operands. In JavaScript, numbers are stored as 64-bit floating point number. JavaScript converts the numbers to 32-bit signed integer before performing the operation. After bitwise operation, it converts the result to 64-bits number.
There are seven bitwise operators in JavaScript. Following is the list of bitwise operators with description.
Operator | Name | Description |
---|---|---|
& | Bitwise AND | Returns 1 if both bits are 1, otherwise 0. |
| | Bitwise OR | Returns 1 if either bit is 1, otherwise 0. |
^ | Bitwise XOR | Returns 1 if both bits are different, otherwise 0. |
! | Bitwise NOT | Returns 1 if bit is 0, otherwise 0. |
<< | Left Shift | Shifts the bits left by pushing zeros in from right and discarding leftmost bits. |
>> | Right Shift | Shifts the bits right by pushing copies of leftmost bit in from left and discarding rightmost bits. |
>>> | Right Shift with Zero | Shifts the bits right by pushing zeros in from left and discarding rightmost bits. |
JavaScript Bitwise AND (&) Operator
The bitwise AND (&) operator performs AND operation on each pair of bits of its integer operands. After the operation, it returns a new integer value with the updated bits.
When bitwise AND operator is applied on a pair of bits, it returns 1 if both bits are 1, otherwise returns 0.
Following is the truth table for bitwise AND operation −
A | B | A & B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Let's understand bitwise AND operation taking an example of 4-bit operands.
A | B | A & B |
---|---|---|
1111 | 0001 | 0001 |
1111 | 0010 | 0010 |
1111 | 0100 | 0100 |
1111 | 1000 | 1000 |
Example
Let's perform bitwise AND (&) operation on 5 and 7. These numbers are represented as 32-bits integer.
Decimal Number | Binary Equivalent (32-bits) |
---|---|
5 | 00000000000000000000000000000101 |
7 | 00000000000000000000000000000111 |
5 & 7 | 00000000000000000000000000000101 (= 5) |
The resultant value of the OR operation of each bit of the 101 and 111 binary numbers is the same as below.
- 1 & 1 = 1
- 1 & 0 = 0
- 1 & 1 = 1
So, the resultant binary number is 111, which is equal to 7 in the decimal representation.
<html> <body> <div id="output"></div> <script> const a = 5; const b = 7; document.getElementById("output").innerHTML = "a & b = " + (a & b); </script> </body> </html>
It will produce the following result −
a & b = 5
JavaScript Bitwise OR (|) Operator
The bitwise OR (|) operator performs OR operation on each pair of bits of its integer operands. After the operation, it returns an integer value with the updated bits.
When bitwise OR operator is applied on a pair of bits, it returns 1 if either of bits is 1, otherwise returns 0.
Following is the truth table for bitwise OR operation.
A | B | A | B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Let's understand bitwise OR operation taking an example of 4-bit operands.
A | B | A | B |
---|---|---|
1111 | 0001 | 1111 |
1111 | 0010 | 1111 |
1111 | 0100 | 1111 |
1111 | 1000 | 1111 |
Example
Let's perform bitwise OR (|) operation on 5 and 7. These numbers are represented as 32-bits integer.
Decimal Number | Binary Equivalent (32-bits) |
---|---|
5 | 00000000000000000000000000000101 |
7 | 00000000000000000000000000000111 |
5 | 7 | 00000000000000000000000000000111 (= 7) |
The resultant value of the OR operation of each bit of the 101 and 111 binary numbers is the same as below.
- 1 | 1 = 1
- 1 | 0 = 1
- 1 | 1 = 1
So, the resultant binary number is 111, which is equal to 7 in the decimal representation.
<html> <body> <div id="output"></div> <script> const a = 5; const b = 7; document.getElementById("output").innerHTML = "a | b = " + (a | b); </script> </body> </html>
It will produce the following result −
a | b = 7
JavaScript Bitwise XOR (^) Operator
The bitwise XOR (^) operator performs exclusive OR operation on each pair of bits of its integer operands. After the operation, it returns an integer value with the updated bits.
When bitwise XOR operator is applied on a pair of bits, it returns 1 if both bits are different, otherwise returns 0.
Following is the truth table for Bitwise XOR operation −
A | B | A ^ B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Example
Let's perform bitwise XOR (^) operation on 5 and 7.
Decimal Number | Binary Equivalent (32-bits) |
---|---|
5 | 00000000000000000000000000000101 |
7 | 00000000000000000000000000000111 |
5 ^ 7 | 00000000000000000000000000000010 (= 2) |
After performing the bitwise XOR operation of 101 and 111, the resultant binary number is given below.
- 1 ^ 1 = 0
- 1 ^ 0 = 1
- 1 ^ 1 = 0
So, the resultant binary number is 010, which is equal to 2.
<html> <body> <div id="output"></div> <script> const a = 5; const b = 7; document.getElementById("output").innerHTML = "a ^ b = " + (a ^ b); </script> </body> </html>
It will produce the following output −
a ^ b = 2
JavaScript Bitwise NOT (~) Operator
The bitwise NOT (~) operator performs the NOT operation on each bit of the binary number. It is a unary operator that inverts each bit of the binary number and returns the 2’s complement to the binary number.
Following is the truth table for the Bitwise XOR operation.
Input (A) | Output (~A) |
---|---|
0 | 1 |
1 | 0 |
Example
Let's perform bitwise NOT (~) operation.
Decimal Number | Binary Equivalent (32-bits) |
---|---|
5 | 00000000000000000000000000000101 |
7 | 00000000000000000000000000000111 |
~5 | 11111111111111111111111111111010 (= -6) |
~7 | 11111111111111111111111111111000 (= -8) |
Try to execute the below code −
<html> <body> <div id="output"></div> <script> const a = 5; const b = 7; document.getElementById("output").innerHTML = "~a = " + (~a) + "<br>" + "~b = " + (~b) </script> </body> </html>
It will produce the following output −
~a = -6 ~b = -8
Bitwise Left Shift (<<) Operator
The JavaScript bitwise left shift (<<) operator moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros from the right and left most bits are discarded.
Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on.
Example
When you left shift 5 (101) by 1, a value becomes 10 (1010). When you perform the left shift operation by 2 places, the resultant value is 20 (10100).
Decimal Number | Binary Equivalent (32-bits) |
---|---|
5 | 00000000000000000000000000000101 |
5 << 1 | 00000000000000000000000000001010 (= 10) |
5 << 2 | 00000000000000000000000000010100 (= 20) |
The following JavaScript program demonstrates the bitwise left shift operation −
<html> <body> <div id="output"></div> <script> const a = 5; document.getElementById("output").innerHTML = "a << 1 = " + (a << 1) + "<br>" + "a << 2 = " + (a << 2); </script> </body> </html>
It will produce the following output −
a << 1 = 10 a << 2 = 20
Bitwise Right Shift (>>) Operator
The bitwise right shift (>>) operator moves all the bits in its first operand to the right by the number of places specified in the second operand. It inserts copies of leftmost bit in from left and discard rightmost bits. In this way it preserves the sign of the number.
In short, it removes the N last bits from the number. Here, N is a second operand. Right-shifting the binary number is equivalent to dividing the decimal number by 2.
Example
In the below example, when we perform the right shift operation on 101 for the first time, the value of a becomes equal to 010. After performing the right-shift operation for the second time, the resultant value is 001, equal to 1 in the decimal representation.
Decimal Number | Binary Equivalent (32-bits) |
---|---|
5 | 00000000000000000000000000000101 |
5 >> 1 | 00000000000000000000000000000010 (= 2) |
~5 | 11111111111111111111111111111010 (= -6) |
~5 >>1 | 11111111111111111111111111111101 (= -3) |
Try to execute the following program −
<html> <body> <div id="output"></div> <script> const a = 5; document.getElementById("output").innerHTML = "a >> 1 = " + (a >> 1) + "<br>" + "~a >> 1 = " + (~a >> 1); </script> </body> </html>
It will produce the following output −
a >> 1 = 2 ~a >> 1 = -3
Bitwise Right Shift with Zero (>>>) Operator
The Right Shift with Zero (>>>) operator is very similar to the right shift operator. It always fills the left bits with zero without worrying about the sign of the bit.
Example
Here, the binary representation of 10 is 1010. When we perform the right shift with zero operation, it moves all bits 2 times in the right direction and inserts two 0 at the start. So, the resultant value will be 0010, equal to 1.
Decimal Number | Binary Equivalent (32-bits) |
---|---|
5 | 00000000000000000000000000000101 |
5 >>> 1 | 00000000000000000000000000000010 (= 2) |
The following JavaScript program demonstrate the use of >>> operator.
<html> <body> <div id="output"></div> <script> const a = 5; document.getElementById("output").innerHTML = "a >>> 1 = " + (a >>> 1); </script> </body> </html>
It will produce the following result −
a >>> 1 = 2
You may try to use the different inputs with each operator and observe the output for more practices.