Tutorials References Menu

JS Tutorial

JS HOME JS Introduction JS Where To JS Output JS Statements JS Syntax JS Comments JS Variables JS Let JS Const JS Operators JS Arithmetic JS Assignment JS Data Types JS Functions JS Objects JS Events JS Strings JS String Methods JS String Search JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Array Iteration JS Array Const JS Dates JS Date Formats JS Date Get Methods JS Date Set Methods JS Math JS Random JS Booleans JS Comparisons JS Conditions JS Switch JS Loop For JS Loop For In JS Loop For Of JS Loop While JS Break JS Typeof JS Type Conversion JS Bitwise JS RegExp JS Errors JS Scope JS Hoisting JS Strict Mode JS this Keyword JS Arrow Function JS Classes JS JSON JS Debugging JS Style Guide JS Best Practices JS Mistakes JS Performance JS Reserved Words

JS Objects

Object Definitions Object Properties Object Methods Object Display Object Accessors Object Constructors Object Prototypes Object Reference Object Map() Object Set()

JS Functions

Function Definitions Function Parameters Function Invocation Function Call Function Apply Function Closures

JS Classes

Class Intro Class Inheritance Class Static

JS Async

JS Callbacks JS Asynchronous JS Promises JS Async/Await

JS Versions

JS Versions JS 2009 (ES5) JS 2015 (ES6) JS 2016 JS 2017 JS 2018 JS IE / Edge JS History

JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM Forms DOM CSS DOM Animations DOM Events DOM Event Listener DOM Navigation DOM Nodes DOM Collections DOM Node Lists

JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS Web APIs

Web API Intro Web Forms API Web History API Web Storage API Web Worker API Web Fetch API Web Geolocation API

JS AJAX

AJAX Intro AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

JS JSON

JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Parse JSON Stringify JSON Objects JSON Arrays JSON Server JSON PHP JSON HTML JSON JSONP

JS vs jQuery

jQuery Selectors jQuery HTML jQuery CSS jQuery DOM

JS Examples

JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor

JS References

JavaScript Objects HTML DOM Objects


JavaScript Bitwise Operations


JavaScript Bitwise Operators

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

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

One bit example:
OperationResult
0 & 00
0 & 10
1 & 00
1 & 11
4 bits example:
OperationResult
1111 & 00000000
1111 & 00010001
1111 & 00100010
1111 & 01000100

Bitwise OR

When a bitwise OR is performed on a pair of bits, it returns 1 if one of the bits are 1:

One bit example:
OperationResult
0 | 00
0 | 1
1 | 01
1 | 11
4 bits example:
OperationResult
1111 | 00001111
1111 | 00011111
1111 | 00101111
1111 | 01001111

Bitwise XOR

When a bitwise XOR is performed on a pair of bits, it returns 1 if the bits are different:

One bit example:
OperationResult
0 ^ 00
0 ^ 1
1 ^ 01
1 ^ 1
4 bits example:
OperationResult
1111 ^ 00001111
1111 ^ 00011110
1111 ^ 00101101
1111 ^ 01001011

JavaScript Bitwise AND (&)

Bitwise AND returns 1 only if both bits are 1:

DecimalBinary
500000000000000000000000000000101
100000000000000000000000000000001
5 & 100000000000000000000000000000001 (1)

Example

let x = 5 & 1;
Try it Yourself »

JavaScript Bitwise OR (|)

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

DecimalBinary
500000000000000000000000000000101
100000000000000000000000000000001
5 | 100000000000000000000000000000101 (5)

Example

let x = 5 | 1;
Try it Yourself »

JavaScript Bitwise XOR (^)

Bitwise XOR returns 1 if the bits are different:

DecimalBinary
500000000000000000000000000000101
100000000000000000000000000000001
5 ^ 100000000000000000000000000000100 (4)

Example

let x = 5 ^ 1;
Try it Yourself »

JavaScript Bitwise NOT (~)

DecimalBinary
500000000000000000000000000000101
~511111111111111111111111111111010 (-6)

Example

let x = ~5;
Try it Yourself »

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:

DecimalBinary
500000000000000000000000000000101
5 << 100000000000000000000000000001010 (10)

Example

let x = 5 << 1;
Try it Yourself »

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:

DecimalBinary
-511111111111111111111111111111011
-5 >> 111111111111111111111111111111101 (-3)

Example

let x = -5 >> 1;
Try it Yourself »

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:

DecimalBinary
500000000000000000000000000000101
5 >>> 100000000000000000000000000000010 (2)

Example

let x = 5 >>> 1;
Try it Yourself »

Binary Numbers

Binary numbers with only one bit set is easy to understand:

Binary RepresentationDecimal value
000000000000000000000000000000011
000000000000000000000000000000102
000000000000000000000000000001004
000000000000000000000000000010008
0000000000000000000000000001000016
0000000000000000000000000010000032
0000000000000000000000000100000064

Setting a few more bits reveals the binary pattern:

Binary RepresentationDecimal value
000000000000000000000000000001015 (4 + 1)
0000000000000000000000000000110113 (8 + 4 + 1)
0000000000000000000000000010110145 (32 + 8 + 4 + 1)

JavaScript binary numbers are stored in two's complement format.

This means that a negative number is the bitwise NOT of the number plus 1:

Binary RepresentationDecimal value
000000000000000000000000000001015
11111111111111111111111111111011-5
000000000000000000000000000001106
11111111111111111111111111111010-6
0000000000000000000000000010100040
11111111111111111111111111011000-40

Converting Decimal to Binary

Example

function dec2bin(dec){
  return (dec >>> 0).toString(2);
}
Try it Yourself »

Converting Binary to Decimal

Example

function bin2dec(bin){
  return parseInt(bin, 2).toString(10);
}
Try it Yourself »