Operator ~ NOT in JavaScritp (basic usage examples)

1

How to understand with simple usage examples the BItwise NOT operator? understand it as if you would like to know someone who is learning about bit by bit operators in Javascript

Thank you!

    
asked by Drako 14.09.2018 в 02:20
source

1 answer

2

To give you an example, first of all you have to handle binary representations very well: If you apply NOT to number 2

var numDos= ~2;

In binary the two is 010, and what the NOT does is invert the bits, that is, we have the reverse: 101. This returns you -3
Another example:

var numDiez= ~10;

This returns -11. We start from that 10 in binary is 0000001010. And inverting the bits are: 1111110101, this is -11. Do not worry about the representation of negative numbers is just a notation. What you should know is, when applying the NOT to any number, let's call it X. What you will get is always a negative X + 1 number, that is - (x + 1).

Aplicas not al numero -16, tienes -(-16+1) = 15
Al numero 18 por ejemplo, - (18+1) -19

And so ..

    
answered by 14.09.2018 в 03:06