-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
Bitwise operators in JavaScript are used to manipulate individual bits of numbers. While most JavaScript operations work with numbers at a higher level (decimal or floating-point), bitwise operators allow you to perform low-level operations directly on the binary representation of integers.
Bitwise operations are often used in performance-critical code, low-level programming, flags, masks, or encryption algorithms. Understanding them can also help you debug certain algorithms efficiently.
All bitwise operations in JavaScript work on 32-bit signed integers, even though JavaScript numbers are generally 64-bit floating-point values.
Before diving into operators, it’s helpful to understand how numbers are represented in binary:
let num = 5; // Decimal: 5
console.log(num.toString(2)); // Logs: 101 (binary representation)
Each number is converted internally to a 32-bit signed integer when performing bitwise operations.
Negative numbers use two’s complement representation.
&
The AND operator compares each bit of two numbers and returns 1
if both bits are 1
, otherwise 0
.
let a = 5; // 0101
let b = 3; // 0011
let result = a & b;
console.log(result); // Logs: 1 (0001 in binary)
Bitwise AND is often used to mask specific bits.
|
The OR operator compares each bit of two numbers and returns 1
if at least one bit is 1.
let a = 5; // 0101
let b = 3; // 0011
let result = a | b;
console.log(result); // Logs: 7 (0111 in binary)
Bitwise OR is useful for setting specific bits.
^
The XOR (exclusive OR) operator returns 1
if the bits are different, and 0
if they are the same.
let a = 5; // 0101
let b = 3; // 0011
let result = a ^ b;
console.log(result); // Logs: 6 (0110 in binary)
XOR is often used in toggles or encryption.
~
The NOT operator flips every bit of a number. In JavaScript, it also converts the number to a 32-bit signed integer.
let a = 5; // 00000000000000000000000000000101
console.log(~a); // Logs: -6
Formula: ~x = -(x + 1)
Be careful with negative results because of two’s complement representation.
<<
The left shift operator shifts bits to the left by a specified number of positions, filling the right side with zeros.
let a = 5; // 0101
let result = a << 2; // Shift left by 2
console.log(result); // Logs: 20 (10100 in binary)
Equivalent to multiplying by 2^n (here 5 * 2^2 = 20).
>>
The signed right shift operator shifts bits to the right, preserving the sign bit for negative numbers.
let a = 20; // 10100
let result = a >> 2;
console.log(result); // Logs: 5 (0101 in binary)
Equivalent to dividing by 2^n and rounding towards negative infinity.
>>>
The unsigned right shift operator shifts bits to the right, filling the left side with zeros, ignoring the sign.
let a = -20;
let result = a >>> 2;
console.log(result); // Logs: 1073741819
This is useful when working with unsigned 32-bit integers.
Bitwise operators can be combined to perform complex operations like flags, masks, and toggles.
const READ = 1; // 0001
const WRITE = 2; // 0010
const EXECUTE = 4; // 0100
let permissions = 0;
// Grant read and write permissions
permissions = permissions | READ | WRITE;
console.log(permissions); // Logs: 3 (0011)
// Check if WRITE permission exists
let hasWrite = (permissions & WRITE) !== 0;
console.log(hasWrite); // true
// Remove READ permission
permissions = permissions & ~READ;
console.log(permissions); // Logs: 2 (0010)
Using bitwise operators for flags is highly efficient.
let value = 5; // 0101
// Toggle the second bit using XOR
value = value ^ 2;
console.log(value); // Logs: 7 (0111)
// Toggle again
value = value ^ 2;
console.log(value); // Logs: 5 (0101)
XOR is ideal for toggling bits without affecting other bits.
All bitwise operations convert operands to 32-bit signed integers.
Use >>>
for unsigned operations, especially with large numbers or negative values.
Bitwise operators are fast and memory-efficient for flags, masks, and low-level logic.
Combining bitwise operations can optimize certain algorithms like permissions, graphics, and cryptography.
JavaScript bitwise operators work at the binary level of 32-bit integers.
&
(AND), |
(OR), ^
(XOR), ~
(NOT) manipulate bits.
<<
, >>
, >>>
shift bits left or right.
Useful in flags, masks, toggles, encryption, and performance-critical code.
Understanding binary representation is key to mastering bitwise operations.
Bitwise operations may not be used daily in typical web apps, but they are essential in advanced JavaScript, game development, low-level computations, and algorithm optimization.
Use the bitwise AND &
operator to check which bits are common between 12
and 5
and log the result.
Use the bitwise OR |
operator to combine 8
and 3
and log the resulting number.
Use the bitwise XOR ^
operator on 7
and 10
and log the result. Explain why the output is what it is.
Use the bitwise NOT ~
operator on the number 6
and log the result.
Left shift the number 4
by 3 positions using <<
and log the result.
Right shift the number 32
by 2 positions using >>
and log the result.
Use the unsigned right shift >>>
operator on -16
and log the result. Explain the difference from >>
.
Implement a simple permission system using bitwise operators: define READ = 1
, WRITE = 2
, EXECUTE = 4
, then grant READ
and EXECUTE
permissions and log the resulting value.
Toggle the second bit of the number 5
using XOR ^
and log the new value. Then toggle it back and log again.
Use bitwise operators to check if a number permissions = 7
has WRITE
permission (value 2
) and log true
or false
.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts