We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 46ab5f3 commit 2fba5d0Copy full SHA for 2fba5d0
1009-complement-of-base-10-integer.js
@@ -14,3 +14,31 @@ const bitwiseComplement = function (N) {
14
// flip all bits
15
return bitmask ^ N
16
}
17
+
18
+// another
19
20
+/**
21
+ * @param {number} N
22
+ * @return {number}
23
+ */
24
+const bitwiseComplement = function (N) {
25
+ let X = 1;
26
+ while (N > X) X = X * 2 + 1;
27
+ return N ^ X;
28
+}
29
30
31
32
33
34
35
36
37
+ if (N === 0) return 1
38
+ // l is a length of N in binary representation
39
+ const l = Math.floor(Math.log(N) / Math.log(2)) + 1
40
+ // bitmask has the same length as num and contains only ones 1...1
41
+ const bitmask = (1 << l) - 1
42
+ // flip all bits
43
+ return bitmask ^ N
44
0 commit comments