Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

JavaScript Bitwise Operators

by suresh

The JavaScript Bitwise Operators perform bit operations. All the decimal values converted into binary values (sequence of bits, i.e., 0100, 1100, 1000, 1001, etc.). Next, JavaScript bitwise operator will work on these bits such as shifting them left to right or converting bit value from 0 to 1, etc.

The below table shows the different Bitwise operators in JavaScript and their meaning. For example, Consider x = 6 and y = 8 and their values in binary form are: x = 0110 and y = 1000

JavaScript Bitwise OperatorsMeaning of operatorsExamples
&Bitwise ANDX & Y = 0000
|Bitwise ORX | Y = 1110
^Bitwise exclusive ORX ^ Y = 1110
~Bitwise complement~X = 00001001 (Bitwise Not operator will convert all 0 into 1.)
<<Shift leftX << 1 = 00001100 (Bits will move 1 step left. If we use 2 or 3 then they shift accordingly)
>>Shift rightY >> 1 = 00000100

Let us see the Truth Table behind Bitwise Operators in JavaScript Programming Language

xyx & y X | yx ^ y
00000
01011
10011
11110

JavaScript Bitwise Operators Example

Let us see one example for a better understanding. For this JavaScript example, We are using two variables a and b, and their values are 9 and 65. We are going to use these two variables to show you various Bitwise operations in JavaScript Language

<!DOCTYPE html>

<html>
<head>
    <title> JavaScript Bitwise Operators </title>
</head>

<body>
    <h1> JavaScript Bitwise Operators </h1>
<script>
    var a = 9, b = 65;
    
    document.write("Bitwise AND Operator a & b =  " + (a & b));
    document.write("<br \> Bitwise OR Operator a | b =  " + (a | b));
    document.write("<br \> Bitwise EXCLUSIVE OR Operator a ^ b =  " + (a ^ b));
    document.write("<br \> Bitwise NOT Operator ~a =  " + (~a));
    
    document.write("<br \> LEFT SHIFT Operator a << 1 =  " + (a << 1));
    document.write("<br \> RIGHT SHIFT Operator b >> 1 =  " + (b >> 1));
 </script>
</body>
</html>
JavaScript Bitwise Operators

In this JS Bitwise Operators program, we declared 2 integers a and b and assigned the values 9 and 65. The binary form of 9 = 0001001 and 65 = 1000001.

The below statements will perform the bitwise operations on a and b, then they will display the output

 document.write("Bitwise AND Operator a & b =  " + (a & b));
 document.write("<br \> Bitwise OR Operator a | b =  " + (a | b));
 document.write("<br \> Bitwise EXCLUSIVE OR Operator a ^ b =  " + (a ^ b));
 document.write("<br \> Bitwise NOT Operator ~a =  " + (~a));
    
 document.write("<br \> LEFT SHIFT Operator a << 1 =  " + (a << 1));
 document.write("<br \> RIGHT SHIFT Operator b >> 1 =  " + (b >> 1));

Lets see the JavaScript calculations

JavaScript Bitwise AND Operation = a & b

0001001 & 1000001 = 0000001 = 1

JavaScript Bitwise OR Operation = a || b

0001001 || 1000001 = 1001001 = 73

Bitwise Exclusive OR Operation = a ^ b

0001001 ^ 1000001 = 1001000 = 72

Left Shift Operation in JS Bitwise Operator: Left Shifting one position is equal to multiplying the number with 2. It means,

a << 1 = (9 * 2) = 18

If we say, a<<2 then multiplying the number with 4 and so on

OR

a << 1 = 0001001 << 1 = 0010010 = 18
Right Shift Operation in JS Bitwise Operator = b >> 1

1000001 >> 1 = 0100000 = 32

Placed Under: JavaScript

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy