Javascript Bitwise Operator NOT

Introduction

The bitwise NOT is represented by a tilde (~) and returns the one's complement of the number.

Consider this example:

let num1 = 25;       // binary 00000000000000000000000000011001 
let num2 = ~num1;    // binary 11111111111111111111111111100110 
console.log(num2);   // -26 

Here, the bitwise NOT operator is used on 25, producing -26 as the result.

This is the end effect of the bitwise NOT: it negates the number and subtracts 1.

The same outcome is produced with the following code:

let num1 = 25;  /*from w ww .j  av  a  2  s .  c om*/
let num2 = -num1 - 1; 
console.log(num2);     // "-26" 



PreviousNext

Related