Ticker

6/recent/ticker-posts

Introduction to JavaScript Bitwise Operations

Introduction to JavaScript Bitwise Operations

Introduction to JavaScript Bitwise Operations

JavaScript provides several bitwise operators that allow you to perform operations at the individual bit level. These operators work with binary representations of numbers and are useful for tasks such as working with flags, optimizing calculations, and performing low-level operations. In this article, we will introduce the bitwise operators available in JavaScript and provide examples to illustrate their usage.

Bitwise Operators in JavaScript

JavaScript provides the following bitwise operators:

  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • ~ (Bitwise NOT)
  • >> (Signed Right Shift)
  • << (Left Shift)
  • >>> (Unsigned Right Shift)

Example:

Consider the following example that demonstrates the usage of bitwise operators in JavaScript:

    
      var a = 5;  // binary: 0101
      var b = 3;  // binary: 0011

      var resultAnd = a & b;   // 0001 (Bitwise AND)
      var resultOr = a | b;    // 0111 (Bitwise OR)
      var resultXor = a ^ b;   // 0110 (Bitwise XOR)
      var resultNot = ~a;      // 1010 (Bitwise NOT)
      var resultRightShift = a >> 1;   // 0010 (Signed Right Shift)
      var resultLeftShift = a << 1;    // 1010 (Left Shift)
      var resultUnsignedRightShift = a >>> 1;   // 0010 (Unsigned Right Shift)
    
  
Working with JavaScript Bitwise AND Operator

Working with JavaScript Bitwise AND Operator

The bitwise AND operator (&) is used to perform a bitwise AND operation between two operands. It compares the corresponding bits of two numbers and produces a new number where each bit is set to 1 only if both bits are 1. In this article, we will explore the usage of the bitwise AND operator in JavaScript with examples.

Example:

Consider the following example that demonstrates the usage of the bitwise AND operator:

    
      var a = 5;  // binary: 0101
      var b = 3;  // binary: 0011

      var result = a & b;   // 0001

      console.log(result);  // 1
    
  
JavaScript Bitwise OR Operator and Bitwise XOR Operator

JavaScript Bitwise OR Operator and Bitwise XOR Operator

JavaScript provides bitwise OR (|) and bitwise XOR (^) operators for performing bitwise OR and XOR operations respectively. The bitwise OR operator compares the corresponding bits of two numbers and produces a new number where each bit is set to 1 if at least one of the bits is 1. The bitwise XOR operator produces a new number where each bit is set to 1 if the corresponding bits of the operands are different. In this article, we will explore the usage of the bitwise OR and XOR operators in JavaScript with examples.

Example:

Consider the following example that demonstrates the usage of the bitwise OR and XOR operators:

    
      var a = 5;  // binary: 0101
      var b = 3;  // binary: 0011

      var resultOr = a | b;   // 0111
      var resultXor = a ^ b;  // 0110

      console.log(resultOr);  // 7
      console.log(resultXor); // 6
    
  
JavaScript Bitwise NOT Operator and Bitwise Shift Operators

JavaScript Bitwise NOT Operator and Bitwise Shift Operators

JavaScript provides the bitwise NOT (~) operator and bitwise shift operators (>>, <<, >>>) for performing bitwise NOT and bitwise shift operations respectively. The bitwise NOT operator inverts the bits of its operand. The bitwise shift operators move the bits of a number to the left or right. In this article, we will explore the usage of the bitwise NOT operator and bitwise shift operators in JavaScript with examples.

Example:

Consider the following example that demonstrates the usage of the bitwise NOT operator and bitwise shift operators:

    
      var a = 5;  // binary: 0101

      var resultNot = ~a;                    // 1010 (Bitwise NOT)
      var resultRightShift = a >> 1;   // 0010 (Signed Right Shift)
      var resultLeftShift = a << 1;    // 1010 (Left Shift)
      var resultUnsignedRightShift = a >>> 1;   // 0010 (Unsigned Right Shift)

      console.log(resultNot);                // -6
      console.log(resultRightShift);         // 2
      console.log(resultLeftShift);          // 10
      console.log(resultUnsignedRightShift); // 2
    
  

Post a Comment

0 Comments