Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).
- Input format
- Single line containing one 32-bit unsigned integer N.
- Output format
- Single line containing number of 1 bits in N.
- Sample Input 1
- 5
- Sample Output 1
- 2
- Explanation 1
- Binary representation of 5 is:
- 101
- Hence total number of 1 bits = 2
- Constraints
- 0<=N<=2^32-1
- */
- /**
- * @param {number} n - a positive integer
- * @return {number}
- */
- function numberOfOneBits(n) {
- // Implement it here
- let count=0;
- while(n){
- if(n&1)
- count++
- n=n>>>1;
- }
- return count;
- }
- function main() {
- let n = parseInt(readLine(), 10);
- let answer = numberOfOneBits(n);
- print(answer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement