# Javascript Bitwise Operators

## Bitwise NOT

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

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

``````

The code above generates the following result.

## Bitwise AND

The bitwise AND operator is represented by the ampersand character `&` and works on two values.

Bitwise AND uses the rules in the following truth table to perform an AND operation between the two bits in the same position.

BIT FROM FIRST NUMBERBIT FROM SECOND NUMBERRESULT
111
100
010
000

The result will be 1 only if both bits are 1. If either bit is 0, then the result is 0.

``````
var result = 25 & 3;
console.log(result);       //1

``````

The actual calculation.

``` 25 = 0000 0000 0000 0000 0000 0000 0001 1001
3 = 0000 0000 0000 0000 0000 0000 0000 0011
---------------------------------------------
AND = 0000 0000 0000 0000 0000 0000 0000 0001

```

The code above generates the following result.

## Bitwise OR

The bitwise OR operator is represented by a single pipe character `|` and works on two numbers.

Bitwise OR follows the rules in this truth table:

BIT FROM FIRST NUMBERBIT FROM SECOND NUMBERRESULT
111
101
011
000

A bitwise OR operation returns 1 if at least one bit is 1. It returns 0 only if both bits are 0.

``````
var result = 25 | 3;
console.log(result);       //27

``````

Actual calculation

```25 = 0000 0000 0000 0000 0000 0000 0001 1001
3 = 0000 0000 0000 0000 0000 0000 0000 0011
--------------------------------------------
OR = 0000 0000 0000 0000 0000 0000 0001 1011

```

The code above generates the following result.

## Bitwise OR Example 2

The following code shows how to use Binary Flags with binary operators.

``````
var FIELD_A = 0x1;  // 00001
var FIELD_B = 0x2;  // 00010
var FIELD_C = 0x4;  // 00100
var FIELD_D = 0x8;  // 01000
var FIELD_E = 0x10; // 10000
/*from  w  ww.jav a 2  s  . com*/
var fieldsSet = FIELD_A | FIELD_C | FIELD_E;

if ((fieldsSet & FIELD_A) && (fieldsSet & FIELD_C)) {
console.log("Fields A and C are set");
}
``````

The code above generates the following result.

## Bitwise XOR

The bitwise XOR operator is represented by a caret `^` and works on two values.

Here is the truth table for bitwise XOR:

BIT FROM FIRST NUMBERBIT FROM SECOND NUMBERRESULT
110
101
011
000

Bitwise XOR returns 1 only when exactly one bit has a value of 1, if both bits contain 1, it returns 0.

``````
var result = 25 ^ 3;
console.log(result);    //26

``````

Actual calculation

``` 25 = 0000 0000 0000 0000 0000 0000 0001 1001
3 = 0000 0000 0000 0000 0000 0000 0000 0011
---------------------------------------------
XOR = 0000 0000 0000 0000 0000 0000 0001 1010

```

The code above generates the following result.

## Bitwise Left Shift

The left shift is represented by two less-than signs `<<` and shifts all bits in a number to the left by the number of positions given.

For example, if the number 2 (which is equal to 10 in binary) is shifted 5 bits to the left, the result is 64 (which is equal to 1000000 in binary), as shown here:

``````
var oldValue = 2;             //equal to binary 10
var newValue = oldValue << 5; //equal to binary 1000000 which is decimal 64
console.log(newValue);
``````

The left shift fills bits with 0s to make the result a complete 32-bit number.

Note that left shift preserves the sign of the number it's operating on. For instance, if -2 is shifted to the left by five spaces, it becomes -64, not positive 64.

The code above generates the following result.

## Bitwise Signed Right Shift

The signed right shift is represented by two greater-than signs `>>` and shifts all bits in a 32-bit number to the right while preserving the sign.

A signed right shift is the exact opposite of a left shift.

``````
var oldValue = 64;               //equal to binary 1000000
var newValue = oldValue >> 5;    //equal to binary 10 which is decimal 2
console.log(newValue);
``````

Javascript fills these empty bits with the value in the sign bit to create a complete number.

The code above generates the following result.

## Bitwise Unsigned Right Shift

The unsigned right shift is represented by three greater-than signs `>>>` and shifts all bits in a 32-bit number to the right.

For numbers that are positive, the effect is the same as a signed right shift.

``````
var oldValue = 64;               //equal to binary 1000000
var newValue = oldValue >>> 5;   //equal to binary 10 which is decimal 2
console.log(newValue);
``````

The code above generates the following result.