Skip to content

Latest commit

 

History

History
42 lines (24 loc) · 1.33 KB

File metadata and controls

42 lines (24 loc) · 1.33 KB

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