bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 476. Number Complement

476. Number Complement

Leetcode Solutions

Compute Bit Length and Construct-bits Bitmask

  1. Calculate the bit length l of the input number num by using the formula l = floor(log2(num)) + 1.
  2. Construct the bitmask by shifting 1 to the left by l bits and subtracting 1, which gives a sequence of l 1-bits.
  3. Perform an XOR operation between num and the bitmask to get the complement.
  4. Return the result of the XOR operation as the complement of num.
UML Thumbnail

Flip Bit by Bit

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...