class Solution:
def bitwiseComplement(self, n: int) -> int:
if n == 0:
return 1
mask = 0
temp = n
while temp > 0:
mask = (mask << 1) | 1
temp >>= 1
return n ^ mask
class Solution {
public:
int bitwiseComplement(int n) {
if (n == 0) return 1;
int mask = 0, temp = n;
while (temp > 0) {
mask = (mask << 1) | 1;
temp >>= 1;
}
return n ^ mask;
}
};
class Solution {
public int bitwiseComplement(int n) {
if (n == 0) return 1;
int mask = 0, temp = n;
while (temp > 0) {
mask = (mask << 1) | 1;
temp >>= 1;
}
return n ^ mask;
}
}
var bitwiseComplement = function(n) {
if (n === 0) return 1;
let mask = 0, temp = n;
while (temp > 0) {
mask = (mask << 1) | 1;
temp >>= 1;
}
return n ^ mask;
};
Given a non-negative integer n
, return the complement of its base-10 representation. The complement of an integer is created by flipping all the bits in its binary representation (turning 0s into 1s and 1s into 0s), but only considering the bits up to the most significant 1. For example, the complement of 5
(which is 101
in binary) is 010
(which is 2
in decimal).
n
.n
up to its highest set bit.0 <= n <= 10^9
At first glance, this problem seems to ask us to simply flip all bits of a number. However, in most programming languages, flipping all bits (using the bitwise NOT operator ~
) affects all 32 or 64 bits, not just the bits used by n
. We only want to flip the bits up to the most significant 1 in n
's binary representation.
For example, for n = 5
(101
in binary), flipping all bits in a 32-bit integer would result in a large negative number, but we only want 010
(which is 2
). So, we need a way to create a mask that covers just the bits in n
.
The brute-force approach would be to convert n
to a binary string, flip each bit, and convert back to decimal. But this involves string manipulation and is less efficient. A more optimal approach is to use bitwise operations to construct the mask and perform the flip.
Let's break down the steps to solve this problem efficiently using bitwise operations:
n
is 0, its binary representation is 0
, and its complement should be 1
.
n
, all set to 1. For example, if n = 5
(101
), the mask should be 111
(which is 7
).
mask = 0
and temp = n
.temp > 0
, shift mask
left by 1 and add 1 (mask = (mask << 1) | 1
), and shift temp
right by 1 (temp >>= 1
).temp
becomes 0, at which point mask
will have all bits set to 1 up to the highest bit of n
.^
) between n
and mask
to flip just the bits we care about.
n = 5 (101)
and mask = 7 (111)
, so 5 ^ 7 = 2
.n ^ mask
is the complement we want.
This approach uses only bitwise operations and a simple loop, making it efficient and easy to implement in any language.
Let's walk through an example with n = 10
:
n = 10
in binary is 1010
.mask = 15
(1111
in binary).
n ^ mask
:
10 ^ 15 = 5
1010 ^ 1111 = 0101
(which is 5)5
.n
to a binary string, flip each bit, and convert back.n
(up to 32 for 32-bit integers).n
.
To find the complement of a base-10 integer, we focus on flipping only the bits up to the most significant 1. The key insight is to construct a bitmask that matches the length of n
's binary representation and use XOR to flip the relevant bits. This approach is both efficient and elegant, relying solely on bitwise operations and a simple loop. It avoids unnecessary string manipulation and works in constant space, making it suitable for large inputs.