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

Appearance settings

Latest commit

 

History

History
History
38 lines (28 loc) · 1.02 KB

File metadata and controls

38 lines (28 loc) · 1.02 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Leetcode #191
// Language: Javascript
// Problem: https://leetcode.com/problems/number-of-1-bits/
// Author: Chihung Yu
// Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
// For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.
// Credits:
// Special thanks to @ts for adding this problem and creating all test cases.
// Hide Company Tags Microsoft Apple
// Hide Tags Bit Manipulation
// Hide Similar Problems (E) Reverse Bits (E) Power of Two (M) Counting Bits
/**
* @param {number} n - a positive integer
* @return {number}
*/
var hammingWeight = function(n) {
if(n === null){
return n;
}
count = 0;
while(n !== 0){
n = n & (n-1);
// use x & (x-1) to determine if an integer is a power of two
// every time you perform the operation x & (x-1), a single 1 bit is erased
count++;
}
return count;
};
Morty Proxy This is a proxified and sanitized view of the page, visit original site.