Javascript - Operator Bitwise NOT

Introduction

The bitwise NOT is represented by a tilde (~).

It returns the one's complement of the number.

Consider this example:

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

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:

Demo

var num1 = 25;  
var num2 = -num1 - 1; 
console.log(num2);               //"-26"

Result

Related Topic