Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit46362e3

Browse files
chore: count set bits using bitwise ops (TheAlgorithms#1532)
* Update BinaryCountSetBits.js* Use `let` instead of `var`---------Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
1 parent628c5ae commit46362e3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

‎Bit-Manipulation/BinaryCountSetBits.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ function BinaryCountSetBits(a) {
1414

1515
if(!Number.isInteger(a))thrownewTypeError('Argument not an Integer')
1616

17-
// convert number into binary representation and return number of set bits in binary representation
18-
returna.toString(2).split('1').length-1
17+
letcount=0
18+
while(a){
19+
a&=(a-1)
20+
count++
21+
}
22+
23+
returncount
1924
}
2025

2126
export{BinaryCountSetBits}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp