The bitwise AND (&) operator in JavaScript is used to perform a bitwise AND operation on the individual bits of two binary numbers. It returns a new number where each bit is set (1) only if the corresponding bits in both input numbers are 1.
Let's take an example to understand how the bitwise AND operator works with binary numbers:
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 AND operation is performed on num1 and
num2. The result has a 1 at each position where both input numbers have a 1. So,
5 & 3 results in 1 because the only common bit set to 1 in both binary representations is the 2^0 position.
The bitwise AND operator is often used in programming to mask or isolate specific bits within a number, perform bit-level tests, or clear (set to 0) certain bits in a binary representation.
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 binary numbers. It returns a new number where each bit is set (1) only if the corresponding bits in both input numbers are 1.
Let's take an example to understand how the bitwise AND operator works with binary numbers:
In this example, the AND operation is performed on num1 and num2. The result has a 1 at each position where both input numbers have a 1. So, 5 & 3 results in 1 because the only common bit set to 1 in both binary representations is the 2^0 position.
The bitwise AND operator is often used in programming to mask or isolate specific bits within a number, perform bit-level tests, or clear (set to 0) certain bits in a binary representation.