forked from NKaty/Algorithms-and-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhamming-weight.js
More file actions
38 lines (30 loc) · 923 Bytes
/
hamming-weight.js
File metadata and controls
38 lines (30 loc) · 923 Bytes
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
// Write a function that takes a 32 bits unsigned integer and
// returns the number of '1' bits it has (also known as the Hamming weight).
// Time complexity - O(32)
const hammingWeight = function(n) {
// Turn into an unsigned integer (just in case)
n = n >>> 0;
let count = 0;
for (let i = 0; i < 32; i++) {
count += n & 1;
n = n >> 1;
}
return count;
};
console.log(hammingWeight(11)); // 3
console.log(hammingWeight(4294967285)); // 30
// Time complexity - O(K), where K is the number of ones
// present in the binary form of the given number
const hammingWeightQuick = function(n) {
// Turn into an unsigned integer (just in case)
n = n >>> 0;
let count = 0;
while (n !== 0) {
// n & (n - 1) flips the least-significant bit to 0
n = n & (n - 1);
count++;
}
return count;
};
console.log(hammingWeightQuick(11)); // 3
console.log(hammingWeightQuick(4294967285)); // 30