Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/JavaScript_Advance/bitwise-operaors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Bitwise Operator
Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal,
or octal numbers. For example, the decimal number nine has a binary representation of 1001.
Bitwise operators perform their operations on such binary representations,but they return standard JavaScript numerical values.

Bitwise AND
When a bitwise AND is performed on a pair of bits, it returns 1 if both bits are 1.

`var x = 5 & 1; // outputs 1`

Bitwise OR
Bitwise OR returns 1 if one of the bits are 1:

var x = 5 | 1; // outputs 5

JavaScript Bitwise XOR (^)
Bitwise OR returns 1 if one of the bits are 1:

var x = 5 ^ 1; // outputs 4

JavaScript Bitwise NOT (~)

var x = ~5; // outputs -6

JavaScript (Zero Fill) Bitwise Left Shift (<<)
This is a zero fill left shift. One or more zero bits are pushed in from the right, and the leftmost bits fall off:

var x = 5 << 1; //outputs 10

JavaScript (Sign Preserving) Bitwise Right Shift (>>)
This is a sign preserving right shift. Copies of the leftmost bit are pushed in from the left, and the rightmost bits fall off:

var x = -5 >> 1; // outputs -3

JavaScript (Zero Fill) Right Shift (>>>)
This is a zero fill right shift. One or more zero bits are pushed in from the left, and the rightmost bits fall off:

var x = 5 >>> 1; // outputs 2