The double tilde method is actually something I never thought about, but is worth explaining.
The double tilde "operator" is not as much as an operator as it's a double bitwise negation.
Let's cast '64' to 64 using this method, so we do ~~'64'. First we will evaluate ~'64'. As bitwise operations work on binary, '64' is cast to a number. So ~64.
64 in binary is 01000000. ~ will negate all the bits so it becomes 10111111, which is -65 since numbers in JavaScript are signed. Now we negate it again, which becomes 01000000, which is 64 in decimal.
Correction:
I previously stated that 10111111 is -63, which is incorrect. It's actually -65. Sorry about that.
The double tilde method is actually something I never thought about, but is worth explaining.
The double tilde "operator" is not as much as an operator as it's a double bitwise negation.
Let's cast
'64'to64using this method, so we do~~'64'. First we will evaluate~'64'. As bitwise operations work on binary,'64'is cast to a number. So~64.64in binary is01000000.~will negate all the bits so it becomes10111111, which is-65since numbers in JavaScript are signed. Now we negate it again, which becomes01000000, which is64in decimal.Correction:
I previously stated that
10111111is-63, which is incorrect. It's actually-65. Sorry about that.Thank you. This was informative. I didn't pay attention about how it works while writing. Will note it down.
Thanks for explaining this. When I got to that one, I was wondering what it was. Great explanation.