---
title: "ByteByteGo: Swap Odd and Even Bits"
url: https://perrotta.dev/2025/11/bytebytego-swap-odd-and-even-bits/
last_updated: 2026-01-03
---


[ByteByteGo: Swap Odd and Even Bits](https://bytebytego.com/exercises/coding-patterns/bit-manipulation/swap-odd-and-even-bits):

```python
def swap_odd_and_even_bits(n: int) -> int:
    ans = 0
    shift = 0

    while n > 0:
        ans += ((n & 0b01) << 1) + ((n & 0b10) >> 1) << shift
        n >>= 2
        shift += 2

    return ans
```

