Number Complement Problem & Solution

Given a positive integer num, output its complement number. The complement strategy is to flip the bits of its binary representation.

See the number complement problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")

static const int _=[](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
  int findComplement(int num) {

    // Calculates the length of the binary representation of `num`.
    int len = log2(num) + 1;

    for (int i = 0; i < len; ++i) {

      // Flips each bit by using the xor operator with 1.
      num ^= (1 << i);

    return num;

Start Here

Many paths, there are. Follow yours, you must.