The bitwise NOT (~) operator in JavaScript is a unary operator that's used to invert the bits of a number. It works by changing each 0 bit to 1, and each 1 bit to 0. It's often used to flip the binary representation of a number.
Here's how you can use the bitwise NOT operator in JavaScript:
let num = 5; // Binary representation: 00000101
let invertedNum = ~num; // Binary representation: 11111010
console.log(invertedNum); // Output: -6
In the example above, the ~num operation flips the bits of the number 5 to give you -6 as the result. Just keep in mind that the result is a signed 32-bit integer in two's complement representation, so it may appear as a negative number in some cases.
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 NOT (~) operator in JavaScript is a unary operator that's used to invert the bits of a number. It works by changing each 0 bit to 1, and each 1 bit to 0. It's often used to flip the binary representation of a number.
Here's how you can use the bitwise NOT operator in JavaScript:
In the example above, the ~num operation flips the bits of the number 5 to give you -6 as the result. Just keep in mind that the result is a signed 32-bit integer in two's complement representation, so it may appear as a negative number in some cases.