> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimize XOR Python Solution with Tests

> Tested Python solution for LeetCode 2429 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2429, [Medium](/catalog/medium). Topics: [Greedy](/catalog/topics/greedy), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/minimize-xor/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2429   # by problem number
lcpy gen -s minimize_xor   # by problem name
```

## Problem

Given two positive integers `num1` and `num2`, find the positive integer `x` such that:

* `x` has the same number of set bits as `num2`, and
* The value `x XOR num1` is **minimal**.

Note that XOR is the bitwise XOR operation.

Return the integer `x`. The test cases are generated such that `x` is uniquely determined.

The number of set bits of an integer is the number of `1`'s in its binary representation.

### Examples

```
Input: num1 = 3, num2 = 5
Output: 3
Explanation:
The binary representations of num1 and num2 are 0011 and 0101, respectively.
The integer 3 has the same number of set bits as num2, and the value 3 XOR 3 = 0 is minimal.
```

```
Input: num1 = 1, num2 = 12
Output: 3
Explanation:
The binary representations of num1 and num2 are 0001 and 1100, respectively.
The integer 3 has the same number of set bits as num2, and the value 3 XOR 1 = 2 is minimal.
```

### Constraints

* 1 \<= num1, num2 \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimize_xor/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimize_xor/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log(max(num1, num2)))
    # Space: O(1)
    def minimize_xor(self, num1: int, num2: int) -> int:
        target_bits = bin(num2).count("1")
        x = num1
        cur_bits = bin(x).count("1")

        # Drop the lowest set bits while we have too many.
        while cur_bits > target_bits:
            x &= x - 1
            cur_bits -= 1

        # Otherwise take the lowest clear bits.
        bit = 1
        while cur_bits < target_bits:
            if not x & bit:
                x |= bit
                cur_bits += 1
            bit <<= 1

        return x
```

## Complexity

| Time | Space |
| - | - |
| O(log(max(num1, num2))) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.