


Javascript program to check if two numbers are bit loops of each other
问题陈述 - 我们给出了两个整数,需要检查这两个数字是否是彼此的位循环。
在 JavaScript 中,每个整数都是一个 32 位二进制数,表示 0 和 1。这里,我们需要检查是否旋转了第一个数字的 32 位字符串;我们可以在第一个数字总共 32 次旋转中获得或不获得第二个数字的 32 位字符串。
使用 ToString() 方法检查两个数字是否相互位循环
toString()方法用于将整数转换为32位二进制数字字符串。之后,我们可以在二进制字符串中添加前导零,使其长度为 32 位。接下来,我们可以将数字的二进制字符串与其自身连接起来,并检查第二个数字的二进制字符串是否作为合并字符串的子字符串存在。
语法
用户可以按照以下语法检查连接字符串后两个数字是否相互位循环。
let num1BinaryDouble = num1Binary + num1Binary; let isBitRotation = num1BinaryDouble.includes(num2Binary)
算法
第 1 步 - 使用 toString() 方法并传递 2 作为其参数,将两个数字转换为二进制字符串。
第 2 步 - 接下来,我们需要将两个字符串的大小设置为 32 位。因此,请向两个二进制字符串添加前导零。
步骤 3 - 将 num1 的二进制字符串合并到自身。
步骤 4 - 检查合并后的字符串是否包含 num2 的二进制字符串。如果是,则意味着两个数字都是彼此的位循环。
示例 1
在下面的示例中,checkBitRotations() 函数实现了上述算法,以确保两个数字是否是彼此的位循环。在输出中,用户可以观察到 1 和 2 是彼此的位循环,但 1 和 5 不是。
<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>
使用 For 循环检查两个数字是否相互位循环
在这种方法中,我们将把数字转换为二进制字符串。之后,我们将使用 for 循环获取第一个数字的所有旋转,并将所有旋转与第二个数字进行比较。如果第一个数字的任何旋转与第二个数字匹配,则它们是彼此的位旋转。
语法
用户可以按照下面的语法来匹配第一个数字与第二个数字的所有旋转,并确保它们是彼此的位旋转。
for (let i = 0; i < num1Binary.length; i++) { if (num1Binary === num2Binary) { return true; } num1Binary = num1Binary[num1Binary.length - 1] + num1Binary.substring(0, num1Binary.length - 1); }
在上面的语法中,我们将第一个数字与第二个数字逐一进行比较,如果匹配,则返回 true。
算法
第 1 步 - 使用 toString() 方法将两个数字转换为二进制字符串。
第 2 步 - 现在,附加前导零以使它们的长度相等。
第 3 步 - 使用 for 循环迭代第一个字符串。
第 4 步 - 如果 num1Binary 与 num2Binary 匹配,则返回 true。
步骤 5 - 在 for 循环中,如果第一个数字的当前旋转与第二个数字不匹配,则旋转第一个数字并获得新的旋转。
李>第 6 步 - 继续将下一个轮换与第二个轮换匹配,直到任何轮换匹配。如果任何旋转不匹配,则返回 false。
示例 2
在下面的示例中,我们实现了上述算法来检查位旋转。在这里,我们逐一获取第一个数字的每次旋转,并将它们与第二个数字进行比较。如果任何旋转匹配,我们将返回 true,用户可以在输出中观察到。
<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>
用户学习了两种不同的方法来检查两个数字是否是彼此的位循环。在第一种方法中,我们将第一个字符串与其自身连接起来,并检查第二个数字是否作为子字符串存在。在第二种方法中,我们使用 for 循环找到第一个数字的所有位旋转,并将它们与第二个数字进行匹配。
The above is the detailed content of Javascript program to check if two numbers are bit loops of each other. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig
