Javascript Program to Check if two numbers are bit rotations of each other or not


Problem Statement − We have given two integer numbers and need to check whether the two numbers are bit rotations of each other.

In JavaScript, every integer is a 32-bit binary number which is a representation of 0 and 1. Here, we need to check if we rotate the 32-bit string of the first number; we can achieve the 32-bit string of the second number or not out of a total of 32 rotations of the first number.

Use the ToString() Method to Check if two Numbers are bit Rotations of Each Other

The toString() method is used to convert the integer to a 32-bit binary number string. After that, we can add leading zeros to the binary string to make it of length 32-bit. Next, we can concat the binary string of the number with itself and check if the second number’s binary string exists as a substring of the merged string.

Syntax

Users can follow the syntax below to check if two numbers are bit rotations of each other after concatenating the string.

let num1BinaryDouble = num1Binary + num1Binary;
let isBitRotation = num1BinaryDouble.includes(num2Binary)

Algorithm

  • Step 1 − Use the toString() method and pass 2 as its parameter to convert both numbers to a binary string.

  • Step 2 − Next, we need to make both strings of size 32-bit. So, add leading zeros to both binary strings.

  • Step 3 − Merge the binary string of num1 to itself.

  • Step 4 − Check if the merged string contains the binary string of num2. If yes, it means both numbers are bit rotations of each other.

Example 1

In the example below, the checkBitRotations() function implements the above algorithm to ensure whether two numbers are bit rotations of each other or not. In the output, users can observe that 1 and 2 are bit rotations of each other, but 1 and 5 are not.

<html>
<body>
   <h3>Checking if <i> two numbers are bit rotations of each other or not </i> in JavaScript</h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      let num1 = 1;
      let num2 = 2;
      let num3 = 5;
      function checkBitRotation(num1, num2) {
         let num1Binary = num1.toString(2);
         let num2Binary = num2.toString(2);
         // append remaining zeros at the start of num1BInary and num2Binary to make it's length 32
         while (num1Binary.length < 32) {
            num1Binary = "0" + num1Binary;
         }
         while (num2Binary.length < 32) {
            num2Binary = "0" + num2Binary;
         }
         // double the string
         let num1BinaryDouble = num1Binary + num1Binary;
         // check if num2Binary is present in num1BinaryDouble
         if (num1BinaryDouble.includes(num2Binary)) {
            return true;
         } else {
            return false;
         }
      }
      output.innerHTML += "The " + num1 + " and " + num2 + " are bit rotations of each other " + checkBitRotation(num1, num2) + "<br>";
      output.innerHTML += "The " + num1 + " and " + num3 + " are bit rotations of each other " + checkBitRotation(num1, num3) + "<br>";
   </script>
</body>
</html>

Using the For-loop to Check if two Numbers are bit Rotations of Each Other or Not

In this approach, we will convert the numbers to binary strings. After that, we will use for loop to get all rotations of the first number and compare all rotations with the second number. If any rotation of the first number matches the second number, they are bit rotations of each other.

Syntax

Users can follow the syntax below to match all rotations of the first number with the second number and ensure they are bit rotations of each other.

for (let i = 0; i < num1Binary.length; i++) {
   if (num1Binary === num2Binary) {
      return true;
   }
   num1Binary = num1Binary[num1Binary.length - 1] + num1Binary.substring(0, num1Binary.length - 1);
}

In the above syntax, we compare one by one rotation of the first number with the second number, and if it matches, we return true.

Algorithm

  • Step 1 − Convert both numbers to binary string using the toString() method.

  • Step 2 − Now, append leading zeros to make them of equal length.

  • Step 3 − Use the for loop to iterate through the first string.

  • Step 4 − If num1Binary matches with the num2Binary, return true.

  • Step 5 − In for loop, if the first number’s current rotation doesn’t match the second number, rotate the first number and get a new rotation.

  • Step 6 − Continue to match the next rotation with the second rotation until any rotation matches. If any rotation doesn’t match, return false.

Example 2

In the example below, we have implemented the above algorithm to check bit rotations. Here, we get every rotation of the first number one by one and compare them with the second number. If any rotation matches, we return true which users can observe in the output.

<html>
<body>
   <h3>Checking if <i> two numbers are bit rotations of each other or not </i> in JavaScript</h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      let num1 = 122;
      let num2 = 2147483678;
      let num3 = 1;
      function checkBitRotation(num1, num2) {
         let num1Binary = num1.toString(2);
         let num2Binary = num2.toString(2);
         // adding leading zeros to make both numbers of the same length
         while (num1Binary.length < num2Binary.length) {
            num1Binary = "0" + num1Binary;
         }
         // checking num1Binary and num2Binary are rotations of each other using for loop
         for (let i = 0; i < num1Binary.length; i++) {
            if (num1Binary === num2Binary) {
               return true;
            }
            num1Binary = num1Binary[num1Binary.length - 1] + num1Binary.substring(0, num1Binary.length - 1);
         }
         return false;
      }
      output.innerHTML += "The " + num1 + " and " + num2 + " are bit rotations of each other " + checkBitRotation(num1, num2) + "<br>";
      output.innerHTML += "The " + num1 + " and " + num3 + " are bit rotations of each other " + checkBitRotation(num1, num3) + "<br>";
   </script>
</body>
</html>

Users learned two different approaches to check whether two numbers are bit rotations of each other. In the first approach, we concat the first string with itself and check if the second number existss as a substring. In the second approach, we find all the bit rotations of the first number using for loop and match them with the second number.

Updated on: 24-Apr-2023

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements