I'm programming a simple module which requires the use of Bitwise OR. My plan is to "Bitwise ORing" four numbers:
- 0x00000004
- 0x00000010
- 0x00000040
- 0x80000000
I outputed them on the console, to visualize them in binary instead of hex:
0x00000004.toString(2)
"100"
0x00000010.toString(2)
"10000"
0x00000040.toString(2)
"1000000"
0x80000000.toString(2)
"10000000000000000000000000000000"
So far so good. If I use the Bitwise OR operator on all of them together I reiceive a pretty weird result:
0x00000004 | 0x00000010 | 0x00000040 | 0x80000000
-2147483564
Respectively in binary:
(0x00000004 | 0x00000010 | 0x00000040 | 0x80000000).toString(2)
"-1111111111111111111111110101100"
This seems plain wrong to me, because I actually expected this:
00000000000000000000000000000100
| 00000000000000000000000000010000
| 00000000000000000000000001000000
| 10000000000000000000000000000000
= 10000000000000000000000001010100
Respectively in decimal:
parseInt('10000000000000000000000001010100', 2)
2147483732
or in Hex:
(2147483732).toString(16)
"80000054"
Is there a limitation I didn't know about? (I actually did look up the reference on MDN) Or is this a bug? Or do I misunderstand the use of Bitwise OR?