I am trying to do some bitwise operations in Javascript,
0xff000000 | 0x00aabbcc
What i expect to give me
0xffaabbcc
However, when I run this result, I get the result -5588020. I expect this is due to the fact that bitwise operations in javascript only work on 32-bit numbers, but not all two numbers in the question <= 32bit anyway? Can someone indicate where I am wrong and how can I get the desired result?
I tried the method described in How to make bitwise AND in javascript for variables longer than 32 bits? no luck.
It is not possible to post your own answer, so posting it here so that someone else will post ...
Thanks for the comments, everyone. Turns out the answer on the bitwise confusion of the Javascript operator covers this. It turns out that JavaScript performs bitwise operations with 32-bit SIGNED targets. My | the above was larger than the maximum possible range, so the result was returned as a signed int. The solution is to shift the output right by 0, which apparently tells JS to consider it unsigned again.
(0xff000000 | 0x00aabbcc)>>>0
aaronsnoswell
source share