## JavaScript Bitwise Operators

Example:

```
<script>
var a = 4;
var b = 1;
document.write("A & B = " + (a & b>
```

+ '<br>'>

;
document.write("A | B = " + (a | b>

+ '<br>'>

;
document.write("~A = " + (~a>

+ '<br>'>

;
</script>

Output:

Try it here

JavaScript, like C, C++, Java, Python, and a variety of other languages, allows bit-wise operations. A number is stored as a 64-bit floating-point number in JavaScript, but the bit-wise operation is performed on a 32-bit binary number, i.e. to execute a bit-operation on a 32-bit binary number. JavaScript turns the number to a signed 32-bit binary number, performs the operation, and then converts the result back to a 64-bit number.

The binary AND (&>

operator accepts two operands and is a binary operator. If both bits are set (i.e. 1>

, bit-wise AND (&>

yields 1; otherwise, it returns 0.

A | B | OUTPUT ( A & B > |
---|---|---|

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

The binary operator Bit-Wise OR (|>

accepts two operands and is a binary operator. If any of the operands is set (i.e. 1>

, bit-wise OR (|>

yields 1; otherwise, it returns 0.

A | B | OUTPUT ( A | B > |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 1 |

Bit-Wise XOR (>

accepts two operands and is a binary operator. If both operands are different, bit-wise XOR (>

yields 1; otherwise, it returns 0.

A | B | OUTPUT ( A ^ B > |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

Bit-Wise NOT (>

is a unary operator, which means it only accepts one operand. NOT (>

flips the bits in a bit-wise manner, i.e. 0 becomes 1 and 1 becomes 0.

A | OUTPUT ( ~A > |
---|---|

0 | 1 |

1 | 0 |

**Shift to the left (<<>**

:It's a binary operator, which means it only takes two operands. The number is specified by the first operator, and the number of bits to shift is specified by the second operator. Each bit is shifted to the left, and 0 bits from the right are added. Excess parts from the left are thrown away.

:

A | 6 ( 00000000000000000000000000000110 > |
---|---|

B | 1 ( 00000000000000000000000000000001 > |

OUTPUT ( A << B > | 12 ( 00000000000000000000000000001100 > |

**Sign Propagating Right Shift ( >> >**

:Its a binary operator i.e. it accepts two operand. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bit is shifted towards the right, the overflowing bits are discarded. This is Sign Propagating as the bits which are added from the left depends upon the sign of the number (i.e. 0 if positive and 1 if negative >

:

A | 6 ( 00000000000000000000000000000110 > |
---|---|

B | 1 ( 00000000000000000000000000000001 > |

OUTPUT ( A >> B > |
3 ( 00000000000000000000000000000011 > |

**Zero fill Right Shift ( >>> >**

:Its a binary operator i.e. it accepts two operand. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bits is shifted towards right, the overflowing bits are discarded. 0 bit is added from the left so its zero fill right shift.

:

A | 6 ( 00000000000000000000000000000110 > |
---|---|

B | 1 ( 00000000000000000000000000000001 > |

OUTPUT ( A >>> B > |
3 ( 00000000000000000000000000000011 > |

Output:

Try it here