The ~ operator is the bitwise NOT. It inverts the bits. So undefined is treated as 0, and when inverted is 1111111111111111 which is basically -1 in two's-complement.
So now you have -(-1) which is 1.
If you ~1, you get 1111111111111110 which is -2. -(-2) = 2, and so on.
I've been tinkering with computers since I was a teen. I'm currently pivoting from my current role as Tech Support manager to Full Stack Web Developer. I'm actively seeking employment in the field.
Ahaaa! That's part of what I was failing to understand, is how the two were working together. The minus has the effect of flipping it back to positive. Thanks for the concise explanation!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice.
The
~
operator is the bitwise NOT. It inverts the bits. Soundefined
is treated as0
, and when inverted is1111111111111111
which is basically-1
in two's-complement.So now you have
-(-1)
which is1
.If you
~1
, you get1111111111111110
which is-2
.-(-2) = 2
, and so on.developer.mozilla.org/en-US/docs/W...
en.wikipedia.org/wiki/Two%27s_comp...
Ahaaa! That's part of what I was failing to understand, is how the two were working together. The minus has the effect of flipping it back to positive. Thanks for the concise explanation!