LeetCode 476, Easy. Topics: Bit Manipulation. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:
Problem
The <strong>complement</strong> of an integer is the integer you get when you flip all the <code>0</code>‘s to <code>1</code>‘s and all the <code>1</code>‘s to <code>0</code>‘s in its binary representation.
<ul>
<li>For example, The integer <code>5</code> is <code>“101”</code> in binary and its <strong>complement</strong> is <code>“010”</code> which is the integer <code>2</code>.</li>
</ul>
<p>Given an integer <code>num</code>, return <em>its complement</em>.</p>
Examples
Constraints
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026