Swap bits with their neighbours Task description

Given an integer, swap its (2k–1)-th and 2k-th least significant bits for all integers k&nbsp;>&nbsp;0. This is sequence A057300 in the OEIS.

(The number is assumed to have “infinitely many” leading zeroes. In practice, this simply means prepending a single 0 bit to odd-length numbers.)



This is code-golf, so the shortest code (in bytes) wins.

Test cases

0 -&gt; 0
1 -&gt; 2
9 -&gt; 6
85 -&gt; 170
220 -&gt; 236
1827 -&gt; 2835
47525 -&gt; 30298
Python 2, 26 bytes
lambda n:2*n-3*(4**n/3&amp;n/2)

Bit tricks!
Say n has form ...ghfedcba in binary. Then, we can split it into every other bit as
n   = o + 2*e
n   = ...hgfedcba
o   = ...0g0e0c0a
2*e = ...h0f0d0b0

Then, the bit-switched result s can be expressed as s=2*o+e.
s   = 2*o + e
s   = ...ghefcdab
2*o = ...g0e0c0a0
e   = ...0h0f0d0b

We'd rather compute only one of e and o, so we express o=n-2*e and substitute
s=2*(n-2*e)+e = 2*n-3*e

So, now it remains to express e in terms of n. The number M=4**n/3 has form ...10101010101 in binary, which serves as a mask for the odd digits. The exponent n ensures that M is long enough. Taking the bitwise and of n/2 and this value gives e as desired.
n/2     = ...hgfedcb
M       = ...1010101
n/2 &amp; M = ...h0f0d0b = e

We can instead express e in terms of o e=(n-o)/2, which gives s=(n+o*3)/2, which saves a byte thanks to an optimization from xsot.
lambda n:n+(n&amp;4**n/3)*3&gt;&gt;1