JS Bitwise

JS Tutorial

JS Version

JS Objects

JS Function

JS Classes

JS Async

JS HTML DOM

JS Browser BOM

JS Web API

JS AJAX

JS JSON

JS vs JQUERY

JS Graphics

JavaScript Bitwise Operations

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 performing the bitwise operation, JavaScript converts numbers to 32 bits signed integers.

After performing the bitwise operation, the result is converted back to 64 bits JavaScript numbers.

Bitwise AND

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

One bit example:

Operation

Result

0 & 0

0

0 & 1

0

1 & 0

0

1 & 1

1

4 bits example:

Operation

Result

1111 & 0000

0000

1111 & 0001

0001

1111 & 0010

0010

1111 & 0100

0100

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:

Operation

Result

0 | 0

0

0 | 1

1 | 0

1

1 | 1

1

4 bits example:

Operation

Result

1111 | 0000

1111

1111 | 0001

1111

1111 | 0010

1111

1111 | 0100

1111

Bitwise XOR

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

One bit example:

Operation

Result

0 ^ 0

0

0 ^ 1

1 ^ 0

1

1 ^ 1

4 bits example:

Operation

Result

1111 ^ 0000

1111

1111 ^ 0001

1110

1111 ^ 0010

1101

1111 ^ 0100

1011

JavaScript Bitwise AND (&)

Bitwise AND returns 1 only if both bits are 1:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Bitwise AND</h2>

<p id=”demo”>My First Paragraph.</p>

<script>

document.getElementById(“demo”).innerHTML = 5 & 1;

</script>

</body>

</html>

Output

JavaScript Bitwise AND

My First Paragraph.

JavaScript Bitwise OR (|)

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

Example

<html>

<body>

<h2>JavaScript Bitwise OR</h2>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML = 5 | 1;

</script>

</body>

</html>

Output

JavaScript Bitwise OR

JavaScript Bitwise XOR (^)

Bitwise XOR returns 1 if the bits are different:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Bitwise XOR</h2

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML = 5 ^ 1;

</script>

</body>

</html>

Output

JavaScript Bitwise XOR

JavaScript Bitwise NOT (~)

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Bitwise NOT</h2>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML = ~ 5;

</script>

</body>

</html>

Output

JavaScript Bitwise NOT

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:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Bitwise Left</h2>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML = 5 << 1;

</script>

</body>

</html>

Output

JavaScript Bitwise Left

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:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Sign Preserving Bitwise Right.</h2>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML = -5 >> 1;

</script>

</body>

</html>

Output

JavaScript Sign Preserving Bitwise Right.

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:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Bitwise Right</h2>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML = 5 >>> 1;

</script>

</body>

</html>

Output

JavaScript Bitwise Right

Converting Decimal to Binary

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Convert Decimal to Binary</h2>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML = dec2bin(-5);

function dec2bin(dec){

  return (dec >>> 0).toString(2);

}

</script>

</body>

</html>

Output

JavaScript Bitwise Right

11111111111111111111111111111011

Converting Binary to Decimal

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Convert Binary to Decimal</h2>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML = bin2dec(101);

function bin2dec(bin){

  return parseInt(bin, 2).toString(10);

}

</script>

</body>

</html>

Output

JavaScript Convert Binary to Decimal

5