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

Leetcode Problem 1009. Complement of Base 10 Integer

1009. Complement of Base 10 Integer

Leetcode Solutions

Compute Bit Length and Construct-bit Bitmask

  1. If the input number is 0, return 1 as its complement is 1.
  2. Calculate the bit length l of the input number n (number of bits required to represent n).
  3. Construct a bitmask with l bits all set to 1. This can be done by shifting 1 to the left by l bits and subtracting 1.
  4. Perform an XOR operation between the input number n and the bitmask to get the complement.
  5. Return the result of the XOR operation as the complement of n.
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...