➥ JS Tutorial
- JS HOME
- JS introduction
- JS Where to
- JS Output
- JS Statements
- JS Syntax
- JS Variables
- JS Operators
- JS Assignment
- JS Data Types
- JS Functions
- JS Objects
- JS Events
- JS Sting methods
- JS String Search
- JS Arrays Methods
- JS Arrays
- JS Sorting Arrays
- JS Array Iteration
- JS Math Object
- JS Comparison & Logical Operators
- JS if else and else if
- JS Switch Statement
- JS For Loop
- JS For In
- JS For Of
- JS While Loop
- JavaScript Break
- JS Sets
- JavaScript typeof
- JS Type Conversion
- JS Bitwise Operations
- JS JSON
- JS Best Practices
- JS Reserved Words
- JS Number Methods
➥ JS Objects
➥ JS Functions
➥ JS Classes
➥ JS HTML DOM
- JavaScript HTML DOM
- JS HTML DOM Document
- JS HTML DOM Elements
- Changing HTML
- JS Forms
- JS HTML DOM - CSS
- DOM Events
- HTML DOM Event Listener
➥ JS Browser BOM
➥ JS Web APIs
➥ JS AJAX
➥ JS JSON
Tutorial
JavaScript Bitwise Operations
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shifts left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
>>> | Zero fill right shift | Shifts right by pushing zeros in from the left, and let the rightmost bits fall off |
Examples:
Operation | Result | Same as | Result |
---|---|---|---|
5 & 1 | 1 | 0101 & 0001 | 0001 |
5 | 1 | 5 | 0101 | 0001 | 0101 |
~ 5 | 10 | ~0101 | 1010 |
5 << 1 | 10 | 0101 << 1 | 1010 |
5 ^ 1 | 4 | 0101 ^ 0001 | 0100 |
5 >> 1 | 2 | 0101 >> 1 | 0010 |
5 >>> 1 | 2 | 0101 >>> 1 | 0010 |
JavaScript Uses 32 bits Bitwise Operands
JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers.
Before a bitwise operation is performed, JavaScript converts numbers to 32 bits signed integers.
After the bitwise operation is performed, the result is converted back to 64 bits JavaScript numbers.
The examples above uses 4 bits unsigned binary numbers. Because of this ~ 5 returns 10.
Since JavaScript uses 32 bits signed integers, it will not return 10. It will return -6.
00000000000000000000000000000101 (5)
11111111111111111111111111111010 (~5 = -6)
A signed integer uses the leftmost bit as the minus sign.
Bitwise AND
It is a binary operator i.e. accepts two operands. Bit-wise AND (&) returns 1 if both the bits are set ( i.e 1) and 0 in any other case.
A | B | OUTPUT ( A & B ) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Bit-Wise OR
Its is a binary operator i.e. accepts two operands. Bit-wise OR ( | ) returns 1 if any of the operand is set (i.e. 1) and 0 in any other case.
A | B | OUTPUT ( A | B ) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Bit-Wise XOR ( ^ )
Its is a binary operator i.e. accepts two operands. Bit-wise XOR ( ^ ) returns 1 if both the operands are different and 0 in any other case.
A | B | OUTPUT ( A ^ B ) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Bit-Wise NOT ( ~ )
Its is a unary operator i.e. accepts single operands. Bit-wise NOT ( ~ ) flips the bits i.e 0 becomes 1 and 1 becomes 0.
A | OUTPUT ( ~A ) |
---|---|
0 | 1 |
1 | 0 |
Left Shift ( << )
Its a binary operator i.e. it accepts two operand. The first operator specifies the number and the second operator specifies the number of bits to shift. Each bits are shifted towards left and 0 bits are added from the right. The excess bits from the left are discarded.
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 ) |