Possible Duplicate:
Where would I use a bitwise operator in JavaScript?
In c/c++ bitwise operations are faster than normal(arithmetic) operations(significant atleast in low performance processors). Does the same apply in js? I don’t think as the reason its faster in c is bitwise operations are hardwired and usually are completed in 1 processor cycle. But js runs within browser which doesn’t have any such hardware(registers I mean) access. I am not sure (around 70% sure 🙂 ). What are typical(or some smarter) uses of bitwise operators (especially in js but I would like to know others too). Please correct me if I am wrong anywhere.
Bitwise operators in JS are slow. Really slow compared to C. The reason is that in JS, all numbers are represented as double-precision floating point numbers, so to perform a bitwise operation, the runtime has to convert them to 32-bit integers and back.
That’s not to say they aren’t useful. e.g., Node#compareDocumentPosition returns a bitmask, and
something.length >>> 0is a common way of getting the length property ofsomethingor zero iflengthisn’t a number or isNaN. Also,a / b | 0is a fast way to doMath.floor(a / b), assumingaandbare >= 0.