问题陈述 - 我们给出了两个整数,需要检查这两个数字是否是彼此的位循环。
在 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 id="Checking-if-i-two-numbers-are-bit-rotations-of-each-other-or-not-i-in-JavaScript">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 id="Checking-if-i-two-numbers-are-bit-rotations-of-each-other-or-not-i-in-JavaScript">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!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
