Skip to content

Commit 54cfc2f

Browse files
authored
Day5 - Mock Coding Interview Series - Bit Manipulation
Complete series at https://bit.ly/MockInterviewSeries
1 parent 189e032 commit 54cfc2f

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

LeetCode/476.number-complement.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
int bitwiseComplement(int n) {
4+
if(n == 0) return 1;
5+
int numberOfBits = log2(n) + 1; // 100 log10(100) => 2+1 => 3
6+
int mask = (1 << numberOfBits) - 1; // pow(2, numberOfBits)
7+
return mask ^ n;
8+
}
9+
};

0 commit comments

Comments
 (0)