We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f88cc36 commit 043d7c4Copy full SHA for 043d7c4
1 file changed
โnumber-of-1-bits/seongmin36.jsโ
@@ -0,0 +1,24 @@
1
+/**
2
+toString(2)์ผ๋ก ํด๊ฒฐํ๋ค๊ฐ ๋ค๋ฅธ ๋ถ์ ํ์ด๋ฅผ ๋ณด๊ณ ๋ค์ ํ์ด๋ณด์๋ค.
3
+
4
+'๋นํธ ์ฐ์ฐ์ ํตํ ๊ณ์ฐ'์ด๋ค.
5
+์ปดํจํฐ์์ ๋ชจ๋ ์ธ์ด๋ 2์ง๋ฒ์ผ๋ก ๋ณํ์ด ๋๋๋ฐ, ์ด๋ฅผ ์ด์ฉํ ํ์ด๋ค.
6
+๊ฐ์ฅ ์ค๋ฅธ์ชฝ ๋นํธ๊ฐ 1์ธ ๊ฒฝ์ฐ์ count++, n์ ์ฐ์ธก์ผ๋ก 1์นธ shift.
7
+shift๋ฅผ ํ๋ฉด ๊ฒฐ๊ตญ ๋จ๋ ์ซ์๋ 0์ด๊ธฐ ๋๋ฌธ์ ์ ์ ํ ์กฐ๊ฑด์ผ๋ก ๋ฃจํ๋ฅผ ๋น ์ ธ๋์จ๋ค.
8
9
+๊ตณ์ด 2์ง๋ฒ์ผ๋ก ๋ณํํ์ง ์๊ณ ๋ ๊ณ์ฐํ ์ ์์ด์ ์ด๊ฒ ๋ ์ข์ ํ์ด๋ผ ์๊ฐํ์๋ค.
10
+ */
11
12
+ * @param {number} n
13
+ * @return {number}
14
15
+function hammingWeight(n) {
16
+ let count = 0;
17
18
+ while (n !== 0) {
19
+ count += n & 1;
20
+ n >>>= 1;
21
+ }
22
23
+ return count;
24
+}
0 commit comments