Skip to content

Commit 4f00340

Browse files
committed
Number of 1 Bits
1 parent 653f199 commit 4f00340

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

number-of-1-bits/casentino.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function hammingWeight(n: number): number {
2+
let num = n;
3+
let output = 0;
4+
while (num !== 1) {
5+
if (num % 2 === 1) {
6+
output += 1;
7+
num = (num - 1) / 2;
8+
} else {
9+
num = num / 2;
10+
}
11+
}
12+
if (num === 1) {
13+
output += 1;
14+
}
15+
return output;
16+
}

0 commit comments

Comments
 (0)