Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 46ab5f3

Browse files
authoredOct 6, 2020
Create 1009-complement-of-base-10-integer.js
1 parent dd65e42 commit 46ab5f3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
 

‎1009-complement-of-base-10-integer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
Please sign in to comment.