In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to convert JS decimal to binary, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to convert JS decimal to binary. Let's take a look.
The process of converting decimal to binary can be regarded as the operation of pressing the remaining numbers after each calculation into the stack.
The specific implementation process is as follows
/ / decimal to binary code function dec2bin (decNumber) {/ / define the stack var stack=new Stack () / / push the number into the stack while (decNumber > 0) {/ / 1-get the remainder and push it into the stack stack.push (decNumber%2) / / 2-get the result of the division as the digital decNumber=Math.floor (decNumber/2)} / / Take var result=''while (! stack.isEmpty ()) {/ / Press the top numbers into the array result+=stack.pop ()} / / return the result return result} console.log (dec2bin (1000)) console.log (dec2bin (1000)) console.log (dec2bin (10))
Handwritten ideas:
1. First define a function and define the required conversion numbers passed in
2. Use stack structure (here, stack functions such as pop push need to be encapsulated in advance)
3. Cycle to determine whether the number is greater than 0 (here is to press the number into the stack)
Inside the loop: first take the remainder of the incoming number (in this case, the decimal to binary needs to be divided by 2), and then update the loop operation until the number is less than or equal to 0
4. Define an empty array first
5. Loop to determine whether there are elements in the stack (here is to take the result out of the stack)
Inside the loop: empty array plus elements taken out each time in the stack
The following is the code that encapsulates the stack that can be omitted
Some attributes in function Stack () {/ / stack this.items = [] / / in-stack operation / / 1. Push the element into the stack Stack.prototype.push = function (element) {this.items.push (element)} / / 2. Take the element Stack.prototype.pop = function () {return this.items.pop ()} / / 3 from the stack. Take a look at the top element (without changing the stack structure) Stack.prototype.peek = function () {return this.items [stack-1]} / / 4. Determine whether the stack is empty Stack.prototype.isEmpty = function () {return this.items.length = = 0} / / 5. Get the number of elements in the stack Stack.prototype.size = function () {return this.items.length} / / 6.toString method Stack.prototype.toString = function () {var result =''for (var I = 0; I < this.items.length Result +) {result + = this.items [I]} return result}} this is the end of the article on "how to convert JS decimal to binary". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to convert JS decimal to binary". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.