The bitwise AND (&) operator in JavaScript is used to perform a bitwise AND operation on the individual bits of two numbers. It returns a new number where each bit is set to 1 only if the corresponding bits in both input numbers are 1.
Here's a simple explanation of how the bitwise AND operator works:
For each pair of bits in the same position in the binary representations of the two numbers:
If both bits are 1, the result bit at that position is set to 1.
Otherwise, if at least one of the bits is 0, the result bit at that position is set to 0.
Here's an example:
let num1 = 5; // Binary representation: 00000101
let num2 = 3; // Binary representation: 00000011
let result = num1 & num2; // Binary representation: 00000001
console.log(result); // Output: 1
In this example, the bitwise AND operation is applied to num1 and
num2. The result has a 1 at the 2^0 position because it's the only position where both input numbers have a 1.
The bitwise AND operator is commonly used for tasks like isolating or testing specific bits within a number, setting certain bits to 0 in a binary representation, or performing bitwise operations in various programming scenarios.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The bitwise AND (&) operator in JavaScript is used to perform a bitwise AND operation on the individual bits of two numbers. It returns a new number where each bit is set to 1 only if the corresponding bits in both input numbers are 1.
Here's a simple explanation of how the bitwise AND operator works:
Here's an example:
In this example, the bitwise AND operation is applied to num1 and num2. The result has a 1 at the 2^0 position because it's the only position where both input numbers have a 1.
The bitwise AND operator is commonly used for tasks like isolating or testing specific bits within a number, setting certain bits to 0 in a binary representation, or performing bitwise operations in various programming scenarios.