-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbit_counting.js
More file actions
21 lines (20 loc) · 1.17 KB
/
bit_counting.js
File metadata and controls
21 lines (20 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/******************************************************************************************
* CODEWARS BIT COUNTING CHALLENGE *
* *
* Problem Statement *
* Write a function that takes an integer as input, & returns the number of bits that are *
* equal to one in the binary representation of that number. You can guarantee that input *
* is non-negative. *
* Example: The binary representation of 1234 is 10011010010, so the function should *
* return 5 in this case *
* *
*****************************************************************************************/
var countBits = function (n) {
let countbits = 0;
while (n > 0) {
let rem = parseInt(n % 2);
if (rem == 1) countbits++;
n = parseInt(n / 2);
}
return countbits;
};