How to useBitwise Operators in Javascript

In this approach, we are using the bitwise operation of the right shift. Here, we have to use the bitwise right shift (>>) and simple arithmetic operations to find the maximum number of pieces in N cuts.

Syntax:

operand1 >> operand2

Example: In this example, we will be printing the Maximum number of pieces in N cuts using bitwise operations.

Javascript




let input = 7;
let hCuts = input >> 1;
let vCuts = input - hCuts;
let result = (hCuts + 1) * (vCuts + 1);
console.log(
    "Max pieces for n = " + input + " is " + result);


Output

Max pieces for n = 7 is 20

JavaScript Program to Find Maximum Number of Pieces in N Cuts

We have given a square piece and the total number of cuts available (n), we need to write a JavaScript program to print the maximum number of pieces of equal size that can be obtained with the n cuts. Horizontal and Vertical cuts are allowed. We have given the example for better understanding.

Example:

Input : n = 7
Output : 20

Table of Content

  • Using parseInt() Method
  • Using Bitwise Operators
  • Using Looping
  • Using Recursive Function

So, let’s see each of the approaches with its implementation.

Similar Reads

Approach 1: Using parseInt() Method

...

Approach 2: Using Bitwise Operators

In this approach, we are using the parseInt() method. Here the input is divided by 2 and using the parseInt() method it is assured that it is been treated as an Integer value, then we apply the the formula to get the maximum number of pieces....

Approach 3: Using Looping

...

Approach 4: Using Recursive Function

In this approach, we are using the bitwise operation of the right shift. Here, we have to use the bitwise right shift (>>) and simple arithmetic operations to find the maximum number of pieces in N cuts....