Bitwise Operators

Introduction

Bitwise Operators are used to perform operations directly on the binary (bit-level) representation of numbers. Unlike arithmetic operators, which work with decimal values, bitwise operators convert numbers into binary and manipulate individual bits.

Bitwise operators are mainly used in system programming, performance optimization, graphics programming, network programming, and low-level applications. They are less common in everyday web development but are useful to understand as part of JavaScript fundamentals.

For automation engineers, bitwise operators are rarely used in Selenium or API automation. However, you may encounter them while working with flags, permissions, binary data, or specialized libraries.


What are Bitwise Operators?

Bitwise operators perform operations on the binary representation of integers.

For example:

Decimal: 5

Binary : 00000101

When you use a bitwise operator, JavaScript converts the number to binary, performs the operation bit by bit, and then converts the result back to a decimal number.


Why Do We Need Bitwise Operators?

Bitwise operators help us:

  • Manipulate individual bits.

  • Store multiple Boolean flags efficiently.

  • Perform low-level calculations.

  • Optimize certain mathematical operations.

  • Work with binary data and permissions.


Types of Bitwise Operators

OperatorNameDescription
&Bitwise ANDSets a bit to 1 only if both bits are 1
``Bitwise OR
^Bitwise XORSets a bit to 1 if the bits are different
~Bitwise NOTInverts all bits
<<Left ShiftShifts bits to the left
>>Right ShiftShifts bits to the right (preserves sign)
>>>Unsigned Right ShiftShifts bits to the right and fills with zeros

Understanding Binary Numbers

Before learning bitwise operators, let’s look at decimal and binary values.

DecimalBinary
10001
20010
30011
40100
50101
60110
70111

Bitwise AND (&)

The Bitwise AND operator returns 1 only when both corresponding bits are 1.

Syntax

number1 & number2

Example

console.log(5 & 3);

Binary calculation:

5 = 0101

3 = 0011

-------------
    0001

Output

1

Bitwise OR (|)

The Bitwise OR operator returns 1 if either corresponding bit is 1.

Example

console.log(5 | 3);

Binary calculation:

5 = 0101

3 = 0011

-------------
    0111

Output

7

Bitwise XOR (^)

The Bitwise XOR operator returns 1 only when the bits are different.

Example

console.log(5 ^ 3);

Binary calculation:

5 = 0101

3 = 0011

-------------
    0110

Output

6

Bitwise NOT (~)

The Bitwise NOT operator inverts every bit.

Example

console.log(~5);

Output

-6

JavaScript uses 32-bit signed integers, so the result may be negative.


Left Shift (<<)

The Left Shift operator moves all bits to the left by the specified number of positions.

Syntax

number << positions

Example

console.log(5 << 1);

Binary calculation:

5 = 0101

Shift left by 1

1010

Output

10

Each left shift by one position approximately doubles the number.


Right Shift (>>)

The Right Shift operator moves bits to the right while preserving the sign bit.

Example

console.log(20 >> 2);

Binary calculation:

20 = 10100

Shift right by 2

00101

Output

5

Unsigned Right Shift (>>>)

The Unsigned Right Shift operator shifts bits to the right and fills the leftmost bits with zeros.

Example

console.log(20 >>> 2);

Output

5

This operator behaves differently from >> for negative numbers.


Real-World Example

Suppose different user permissions are stored using bits.

const READ = 1;
const WRITE = 2;
const DELETE = 4;

const permissions = READ | WRITE;

console.log(permissions);

Output

3

This indicates that the user has both READ and WRITE permissions.


Checking whether a permission exists:

console.log((permissions & WRITE) !== 0);

Output

true

Automation Testing Example

Bitwise operators are uncommon in Selenium automation, but you may encounter them when handling permission flags or status codes.

Example:

const EXECUTE = 8;
const permissions = 9;

console.log((permissions & EXECUTE) !== 0);

Output

true

Another example:

const SUCCESS = 1;
const WARNING = 2;

const result = SUCCESS | WARNING;

console.log(result);

Output

3

This combines multiple status flags into a single value.


Common Mistakes

Confusing Logical and Bitwise Operators

Incorrect assumption:

5 && 3

This uses the logical AND operator.

Bitwise AND uses:

5 & 3

Expecting Bitwise Operators to Work with Floating-Point Numbers

Bitwise operators work only with integers.

console.log(5.8 & 3.2);

JavaScript converts both values to integers before performing the operation.


Misunderstanding Bitwise NOT (~)

Many beginners expect:

~5

to produce:

-5

Actual output:

-6

This is because JavaScript uses 32-bit signed integer representation.


Best Practices

Use Bitwise Operators Only When Needed

Most JavaScript applications do not require bitwise operations.


Prefer Readable Code

Avoid bitwise operations if simpler alternatives exist.


Use Constants for Flags

Instead of magic numbers, define constants.

Example:

const READ = 1;
const WRITE = 2;
const DELETE = 4;

Document Complex Bitwise Logic

If bitwise calculations become complex, add comments to explain the logic.


Conclusion

Bitwise operators allow JavaScript developers to manipulate numbers at the binary level. Although they are less common in everyday web development, they are useful for working with flags, permissions, binary data, and low-level operations.

Automation engineers may occasionally encounter bitwise operators while working with APIs, security permissions, or specialized libraries. Understanding how these operators work helps build a strong foundation in JavaScript.


Frequently Asked Questions (FAQs)

What are Bitwise Operators?

Bitwise operators perform operations directly on the binary representation of integers.


What are the bitwise operators available in JavaScript?

  • & (Bitwise AND)

  • | (Bitwise OR)

  • ^ (Bitwise XOR)

  • ~ (Bitwise NOT)

  • << (Left Shift)

  • >> (Right Shift)

  • >>> (Unsigned Right Shift)


What is the difference between && and &?

  • && is a logical AND operator.

  • & is a bitwise AND operator.


Are bitwise operators commonly used in Selenium automation?

No. They are rarely used in Selenium automation but may appear in libraries, permission handling, binary flags, or low-level programming tasks.


Why should JavaScript developers learn bitwise operators?

Understanding bitwise operators helps developers work with binary data, optimize specific calculations, and understand advanced JavaScript concepts.


Key Takeaways

  • Bitwise operators work on the binary representation of integers.

  • JavaScript provides seven bitwise operators.

  • & performs Bitwise AND.

  • | performs Bitwise OR.

  • ^ performs Bitwise XOR.

  • ~ inverts all bits.

  • <<, >>, and >>> shift bits left or right.

  • Bitwise operators are commonly used for flags and permissions.

  • They are less common in everyday web development but important for advanced programming.

  • Understanding bitwise operators strengthens your overall JavaScript knowledge.