Basics
JS Bitwise
JavaScript Bitwise Operators
JavaScript bitwise operators like & and | manipulate binary data.
Introduction to Bitwise Operators
Bitwise operators in JavaScript allow you to perform operations on binary representations of numbers. These operators are particularly useful in scenarios where you need to manipulate individual bits of data, such as in low-level programming tasks, cryptography, and performance-critical applications.
Bitwise operations treat their operands as a set of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. This allows for efficient processing and manipulation of binary data.
Common Bitwise Operators
JavaScript provides several bitwise operators, each with its own unique function:
- & (AND): Performs a bitwise AND operation. It compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, it is set to 0.
- | (OR): Performs a bitwise OR operation. It compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1.
- ^ (XOR): Performs a bitwise XOR operation. It compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1. Otherwise, it is set to 0.
- ~ (NOT): Performs a bitwise NOT operation. It inverts all the bits of the operand, turning 0s into 1s and 1s into 0s.
- << (Left Shift): Shifts all the bits in the operand to the left by the number of positions specified, filling the vacated bits with 0s.
- >> (Right Shift): Shifts all the bits in the operand to the right by the number of positions specified, filling the vacated bits with the sign bit (for signed numbers).
- >>> (Unsigned Right Shift): Shifts all the bits in the operand to the right by the number of positions specified, filling the vacated bits with 0s, regardless of the sign of the number.
Bitwise AND Operator Example
The & operator performs a bitwise AND operation. Here's an example:
Suppose you have two binary numbers: 5 (0101 in binary) and 3 (0011 in binary). The AND operation on these numbers will result in 1 (0001 in binary).
Bitwise OR Operator Example
The | operator performs a bitwise OR operation. Here's an example:
Consider the same numbers, 5 (0101 in binary) and 3 (0011 in binary). The OR operation on these numbers will result in 7 (0111 in binary).
Bitwise XOR Operator Example
The ^ operator performs a bitwise XOR operation. Here's an example:
Using the numbers 5 (0101 in binary) and 3 (0011 in binary), the XOR operation will result in 6 (0110 in binary).
Bitwise NOT Operator Example
The ~ operator performs a bitwise NOT operation. It inverts the bits of the number. Here's a simple example:
Applying the NOT operation on 5 (0101 in binary) will invert the bits, resulting in -6 in JavaScript, due to how negative numbers are represented in two's complement binary form (11111111111111111111111111111010 in 32-bit binary).
Practical Applications of Bitwise Operations
Bitwise operators can be incredibly useful in various fields:
- Performance Optimization: Bitwise operations are typically faster than arithmetic operations, making them useful in performance-critical code.
- Cryptography: Cryptographic algorithms often use bitwise operations for data encryption and decryption processes.
- Graphics: Manipulating pixel data in images often requires bitwise operations for tasks like masks and blending.
- Network Programming: Bitwise operators can efficiently handle protocol header data and flags.
Basics
- Introduction
- Where To
- Output
- Syntax
- Comments
- Variables
- Scope
- Hoisting
- Errors
- Data Types
- Operators
- Ternary Operator
- Short-Circuit Evaluation
- If Else
- Switch
- Loops
- For...Of/For...In
- Functions
- this Keyword
- Objects
- Arrays
- Strings
- Template Literals
- Numbers
- Number Properties
- Dates
- Math
- Booleans
- Type Conversion
- Destructuring
- Spread/Rest
- RegExp
- Strict Mode
- Modules
- Security Basics
- Debugging
- Best Practices
- Mistakes
- Performance
- Reserved Words
- Sets
- Maps
- Bitwise
- Array Const
- Previous
- Maps
- Next
- Array Const