We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent dd65e42 commit 46ab5f3Copy full SHA for 46ab5f3
1009-complement-of-base-10-integer.js
@@ -0,0 +1,16 @@
1
+/**
2
+ * @param {number} N
3
+ * @return {number}
4
+ */
5
+const bitwiseComplement = function (N) {
6
+ if (N === 0) return 1
7
+ // bitmask has the same length as N and contains only ones 1...1
8
+ let bitmask = N
9
+ bitmask |= bitmask >> 1
10
+ bitmask |= bitmask >> 2
11
+ bitmask |= bitmask >> 4
12
+ bitmask |= bitmask >> 8
13
+ bitmask |= bitmask >> 16
14
+ // flip all bits
15
+ return bitmask ^ N
16
+}
0 commit comments