Community for developers to learn, share their programming knowledge. Register!
JavaScript Operators

JavaScript Bitwise Operators


In the realm of programming, mastering JavaScript bitwise operators can significantly enhance your ability to manipulate data at a granular level. This article provides comprehensive training on the intricacies of bitwise operators in JavaScript, equipping you with the knowledge to leverage them effectively in your coding practices.

Introduction to Bitwise Operators

Bitwise operators in JavaScript are used to perform operations on individual bits of numeric values. Unlike regular arithmetic operators, which work with whole numbers, bitwise operators treat numbers as sequences of bits (binary digits). This approach can be particularly useful for tasks such as optimizing performance, manipulating flags, or performing low-level data manipulation. Understanding these operators is essential for intermediate and professional developers looking to deepen their programming expertise.

Bitwise AND Operator (&)

The Bitwise AND operator (&) compares each bit of two numbers and returns a new number formed by bits that are set to 1 in both operands.

Example:

let a = 5;   // Binary: 0101
let b = 3;   // Binary: 0011
let result = a & b; // Binary: 0001 (Decimal: 1)
console.log(result); // Output: 1

In this example, the binary representation of 5 is 0101 and 3 is 0011. The AND operation results in 0001, which is 1 in decimal.

Bitwise OR Operator (|)

The Bitwise OR operator (|) compares each bit of two numbers and returns a new number formed by bits that are set to 1 in either operand.

Example:

let a = 5;   // Binary: 0101
let b = 3;   // Binary: 0011
let result = a | b; // Binary: 0111 (Decimal: 7)
console.log(result); // Output: 7

Here, the OR operation results in 0111, which is 7 in decimal.

Bitwise XOR Operator (^)

The Bitwise XOR operator (^) compares each bit of two numbers and returns a new number formed by bits that are set to 1 in one operand but not both.

Example:

let a = 5;   // Binary: 0101
let b = 3;   // Binary: 0011
let result = a ^ b; // Binary: 0110 (Decimal: 6)
console.log(result); // Output: 6

In this case, the XOR operation results in 0110, which is 6 in decimal.

Bitwise NOT Operator (~)

The Bitwise NOT operator (~) inverts the bits of a number, converting every 1 to 0 and every 0 to 1.

Example:

let a = 5;   // Binary: 0101
let result = ~a; // Binary: 1010 (Decimal: -6)
console.log(result); // Output: -6

The result of inverting 5 is -6, which might seem counterintuitive. This occurs because JavaScript uses the two's complement representation for negative numbers.

Left Shift Operator (<<)

The Left Shift operator (<<) shifts the bits of a number to the left by a specified number of positions, filling the vacated bits with zeros. This operation effectively multiplies the number by powers of two.

Example:

let a = 5;   // Binary: 0101
let result = a << 1; // Binary: 1010 (Decimal: 10)
console.log(result); // Output: 10

Shifting 5 to the left by one position results in 10.

Right Shift Operator (>>)

The Right Shift operator (>>) shifts the bits of a number to the right by a specified number of positions. The sign bit (the leftmost bit) is preserved, which means that this operator performs an arithmetic shift.

Example:

let a = 5;   // Binary: 0101
let result = a >> 1; // Binary: 0010 (Decimal: 2)
console.log(result); // Output: 2

In this example, shifting 5 to the right results in 2.

Unsigned Right Shift Operator (>>>)

The Unsigned Right Shift operator (>>>) also shifts bits to the right but does not preserve the sign bit. Instead, it fills the leftmost bits with zeros, effectively treating the number as an unsigned integer.

Example:

let a = -5;  // Binary: 11111011 (in 32 bits)
let result = a >>> 1; // Binary: 01111101 (Decimal: 2147483645)
console.log(result); // Output: 2147483645

This operator is useful when dealing with binary representations of numbers that may be negative.

Understanding Binary Representation

To effectively utilize bitwise operators, it’s essential to understand binary representation. JavaScript numbers are stored as 64-bit floating point values, but bitwise operations convert these to 32-bit signed integers. In binary, each digit represents a power of two, with the rightmost digit being 2^0, the next 2^1, and so forth.

When performing operations, JavaScript coerces the numbers to 32-bit integers. For example, a number like 257 is represented in binary as 00000001 00000001, and bitwise operations will directly manipulate these bits.

Summary

In conclusion, JavaScript bitwise operators provide powerful tools for manipulating data at the bit level. Whether you are performing calculations, optimizing performance, or handling low-level data structures, understanding these operators is crucial. From the Bitwise AND to Unsigned Right Shift, each operator has its unique application and nuances that can enhance your coding capabilities.

By mastering bitwise operations, you equip yourself with advanced techniques that can lead to more efficient and effective programming solutions. For further exploration, consider diving into the MDN Web Docs on Bitwise Operators for more in-depth information and examples.

Last Update: 16 Jan, 2025

Topics:
JavaScript