Home > Article > Backend Development > Recommended 10 statistical binary source codes (collection)
Those who work on the front end must have few opportunities to come into contact with binary. Aren't those bit operations something that the bottom layer should consider? I saw a question yesterday. It was related to binary, but it didn't matter whether bit operations were used or not. It could be easily solved with the help of the language features of JS. The description is as follows: Write a function that receives a positive decimal integer as a parameter, represents it in binary, and returns the number whose digits are equal to 1. Let's take an example: 1234 is expressed as 10011010010 in binary. There are 5 1's in it, so 5 is returned. After reading this description, the first thing that came to my mind was that my teacher used to teach me how to convert from decimal to binary (the more I use the book, the less I will regret it^_^). Fortunately, I have a good memory, and I quickly thought of taking the remainder of 2 for this positive integer, then dividing it by 2, and then taking the remainder of 2 and dividing it by 2... until the result is 0. Then, in the above process, use a variable to record the number of times the remainder is 1, and finally return. So there is the following approach: var countBits = function(n) { &
1. JavaScript Fun Question: Statistical Binary
# #Introduction: Those who work on the front end must have few opportunities to come into contact with binary. Aren’t those bit operations something that the bottom layer should consider?
【Related Q&A recommendations】:
The above is the detailed content of Recommended 10 statistical binary source codes (collection). For more information, please follow other related articles on the PHP Chinese website!