What is the bitwise NOT (~) operator in JavaScript?
What is the bitwise NOT (~) operator in JavaScript?
Student
Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies. I love to learn new things in life that keep me motivated.
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.