Problem

You’re given an arbitrary 32-bit integer `n`

. Take its binary representation, split bits into it in pairs (bit number `0`

and `1`

, bit number `2`

and `3`

, etc.) and swap bits in each pair. Then return the result as a decimal number.

**Example**

- For
`n = 13`

, the output should be

`swapAdjacentBits(n) = 14`

.`13`

._{10}= 1101_{2}~> {11}{01}_{2}~> 1110_{2}= 14_{10} - For
`n = 74`

, the output should be

`swapAdjacentBits(n) = 133`

.`74`

._{10}= 01001010_{2}~> {01}{00}{10}{10}_{2}~> 10000101_{2}= 133_{10}

Note the preceding zero written in front of the initial number: since both numbers are 32-bit integers, they have`32`

bits in their binary representation. The preceding zeros in other cases don’t matter, so they are omitted. Here, however, it does make a difference.

**Input/Output**

**[time limit] 4000ms (rb)**

**[input] integer n**

**Solution**

((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1)

**Explanation**

Let’s take n = 13, for example.

13_{10} = 1101_{2}

(1101_{2} & 1010_{2}) >> 1 extracts the higher bits and shifts to the lower bit position.

- 1101
_{2}& 1010_{2}= 1000_{2} - 1000
_{2}>> 1 = 0100_{2}

(1101_{2} & 0101_{2}) << 1 extracts the lower bits and shifts to the higher bit position.

- 1101
_{2}& 0101_{2}= 0101_{2} - 0101
_{2}<< 1 = 1010_{2}

So, we combine the two, and we get our swapped bits.

- 0100
_{2}_{ }| 1010_{2}= 1110_{2}