Simplify the inverse of Z X X Y function
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In programming notation, ^ almost always means bitwise XOR, not exponentiation. That changes this problem completely. For z = x ^ (x << y), the inverse is not something you derive with logarithms. You recover it by reconstructing the bits of x in the right order.
Understand the Bit Dependency
Assume y is fixed and you are working with a fixed-width unsigned integer. The expression says that each bit of z is the XOR of:
- the corresponding bit of
x - the bit of
xthat isypositions lower, shifted into place
If bit positions are zero-based, then:
- when
i < y, the shifted term contributes zero, soz_i = x_i - when
i >= y,z_i = x_i xor x_(i-y)
That recurrence is the whole inverse. The low y bits come straight from z, and every later bit can be solved once the earlier bits are known.
Recover x from Low Bits to High Bits
Because a left shift depends on lower-order bits, inversion proceeds from least significant bit upward. That is why this transform shows up in XOR-shift style algorithms: it is simple to apply and still reversible when the width is known.
The critical line is x_bit = z_bit ^ prior_bit. Once you have already reconstructed the earlier bit at i - shift, the current bit follows immediately.
Walk Through a Small Example
Take x = 0b10110110 with y = 3. The first three bits of z match the first three bits of x because there is no shifted contribution yet. Starting at bit index 3, each new bit of z mixes the current bit of x with a previously seen bit of x.
That means inversion is deterministic:
- copy the first
ybits directly - solve each later bit with XOR against the already recovered bit
ypositions earlier
This is much simpler than brute force, and it scales linearly with the word size.
Fixed Width Is Part of the Problem
Python integers are unbounded, but machine-level bit tricks usually assume a fixed width such as 8, 16, 32, or 64 bits. That is why the masking step matters.
If you forget the mask in a fixed-width setting, high bits that should have been discarded can leak into the result and make the inverse look wrong.
Right-Shift Versions Reverse in the Other Direction
The idea changes slightly for z = x ^ (x >> y). In that case, higher bits influence lower bits, so reconstruction goes from the most significant side down to the least significant side.
That gives a useful mental rule:
- invert
x ^ (x << y)from low bits to high bits - invert
x ^ (x >> y)from high bits to low bits
Once you think in terms of bit dependencies rather than algebra, the inverse becomes straightforward.
Common Pitfalls
- Reading
^as exponentiation instead of bitwise XOR. - Forgetting that the inverse assumes a fixed shift amount.
- Ignoring the word width and skipping masking in fixed-width code.
- Recovering bits in the wrong direction for the type of shift used.
- Trying brute force when the bit recurrence gives a direct inverse.
Summary
- '
z = x ^ (x << y)is a reversible XOR-shift transform, not an exponential function.' - The first
ybits ofzare copied directly fromx. - Each later bit can be recovered with
x_i = z_i xor x_(i-y). - Inversion proceeds from low bits to high bits for left shifts.
- Fixed-width masking is essential when the original operation assumes machine-size integers.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.