Skip to content

Commit 8d1a547

Browse files
committed
🆗 [testing] OK, now i understand bit manipulation
1 parent e9f7993 commit 8d1a547

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

191/my_solution.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,21 @@ const hammingWeight = (n) => {
2626
}
2727

2828

29+
hammingWeight(00000000000000000000000000000011)
30+
// 00000000000000000000000000000011
31+
// 11 & 10 = 10
32+
// +1 = 1
33+
34+
// 00000000000000000000000000000010
35+
// 10 & 01 = 0
36+
// +1 = 2
37+
38+
// 00000000000000000000000000000000
39+
// -
40+
41+
42+
// hammingWeight(00000000000000000000000000001011)
2943
// hammingWeight(00000000000000000000000000001011)
3044
// hammingWeight(11111111111111111111111111111101)
31-
hammingWeight(11111111111111111111111111111101)
45+
// hammingWeight(11111111111111111111111111111101)
3246
// hammingWeight(parseInt("11111111111111111111111111111101", 2));

371/my_solution.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44
* @return {number}
55
*/
66
const getSum = (a, b) => {
7-
console.log(`a:${a}, b:${b}`)
8-
console.log(Math.log(1))
9-
console.log(Math.log(2))
10-
a += b;
7+
// TODO: OK TOTALLY DONT GET IT ...
8+
while (b != 0) {
9+
console.log(`--> a:${a}, b:${b}`)
10+
console.log(`a & b :${a & b}`)
11+
12+
let tmp = (a & b) << 1;
13+
console.log(`(a & b << 1) :${a & b << 1}`)
14+
a = a ^ b;
15+
console.log(`(a ^ b): ${a}`)
16+
b = tmp;
17+
console.log(`b:${b}`)
18+
}
19+
console.log("a")
20+
console.log(a)
21+
1122
return a;
1223
}
1324

25+
1426
getSum(1, 2)
27+
getSum(121, 239)

371/solution.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44
* @return {number}
55
*/
66
const getSum = (a, b) => {
7-
// TODO: OK TOTALLY DONT GET IT ...
87
while (b != 0) {
98
console.log(`--> a:${a}, b:${b}`)
109
console.log(`a & b :${a & b}`)
1110

12-
let tmp = (a & b) << 1;
11+
let tmp = (a & b) << 1; // bit shift
1312
console.log(`(a & b << 1) :${a & b << 1}`)
13+
1414
a = a ^ b;
1515
console.log(`(a ^ b): ${a}`)
16+
1617
b = tmp;
1718
console.log(`b:${b}`)
1819
}
20+
1921
console.log("a")
2022
console.log(a)
2123

2224
return a;
2325
}
2426

27+
2528
// getSum(1, 2)
26-
getSum(121, 239)
29+
// getSum(121, 239)

0 commit comments

Comments
 (0)