[Question]
  [
# Task description
Given an integer, swap its **(2k–1)**-th and **2k**-th least significant bits for all integers **k > 0**. This is sequence [A057300](https://oeis.org/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.)
[![enter image description here](https://i.stack.imgur.com/j6sll.jpg)](https://i.stack.imgur.com/j6sll.jpg)
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code (in bytes) wins.
# Test cases
```
0 -> 0
1 -> 2
9 -> 6
85 -> 170
220 -> 236
1827 -> 2835
47525 -> 30298
```
  
[Answer]
## Python 2, 26 bytes
```
lambda n:2*n-3*(4**n/3&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 & 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&4**n/3)*3>>1
```
[Answer]
# C function, 38
Bit-twiddling:
```
f(x){return(x&~0U/3*2)/2+(x&~0U/3)*2;}
```
[Ideone.](https://ideone.com/5HOmv7)
---
Or for the fun of it:
# C recursive function, 43
As per the [OEIS formula](https://oeis.org/A057300), `a(4n+k) = 4a(n) + a(k), 0 <= k <= 3`
```
f(x){return x>3?4*f(x/4)+f(x%4):x%3?3-x:x;}
```
or
```
f(x){return x>3?4*f(x/4)+f(x%4):x%2*2+x/2;}
```
[Ideone.](https://ideone.com/plkr0v)
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), ~~10~~ ~~9~~ 7 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
b4d2UFḄ
```
[Try it online!](http://jelly.tryitonline.net/#code=YjRkMlVG4biE&input=&args=NDc1MjU) or [verify all test cases](http://jelly.tryitonline.net/#code=YjRkMlVG4biECsOH4oKs&input=&args=WzAsIDEsIDksIDg1LCAyMjAsIDE4MjcsIDQ3NTI1XQ).
### How it works
```
b4d2UFḄ  Main link. Argument: n (integer)
b4       Convert n to base 4.
  d2     Divmod each quaternary digit x by 2, yielding [x : 2, x % 2].
    U    Upend; reverse each pair, yielding [x % 2, x : 2].
     F   Flatten the 2D list of binary digits.
      Ḅ  Convert from binary to integer.
```
[Answer]
## CJam, ~~16~~ ~~14~~ 13 bytes
```
ri4b4e!2=f=4b
```
[Test it here.](http://cjam.aditsu.net/#code=ri4b4e!2%3Df%3D4b&input=181)
### Explanation
```
ri  e# Read input and convert to integer.
4b  e# Get base-4 digits.
4e! e# Push all permutations of [0 1 2 3].
2=  e# Select the third one which happens to be [0 2 1 3].
f=  e# For each base-4 digit select the value at that position in the previous
    e# list, which swaps 1s and 2s.
4b  e# Convert back from base 4.
```
[Answer]
# Pyth, 9 bytes
```
iXjQ4S2)4
```
Try it in the [Pyth Compiler](https://pyth.herokuapp.com/?code=iXjQ4S2%294&input=1&test_suite=1&test_suite_input=9%0A85%0A220%0A1827%0A47525&debug=0).
### How it works
```
  jQ4      Convert the input (Q) to base 4.
 X   S2)   Translate [1, 2] to [2, 1].
i       4  COnvert from base 4 to integer.
```
[Answer]
## JavaScript (ES6), ~~32~~ 30 bytes
```
(n,m=0x55555555)=>n*2&~m|n/2&m
```
Only works up to 1073741823 due to the limitations of JavaScript's integers. ~~38~~ 36 bytes works up to 4294967295:
```
(n,m=0x55555555)=>(n*2&~m|n/2&m)>>>0
```
Edit: Saved 2 bytes thanks to @user81655.
51 bytes works up to 4503599627370495:
```
n=>parseInt(n.toString(4).replace(/1|2/g,n=>3-n),4)
```
[Answer]
# x86 asm function: 14 bytes of machine code
## uint64\_t version: 24 bytes
x86-64 SysV calling convention (`x` in `edi`), but this same machine code will also work in 32bit mode. (Where the `lea` will decode as `lea eax, [edi + eax*2]`, [which gives identical results](https://stackoverflow.com/questions/34377711/which-2s-complement-integer-operations-can-be-used-without-zeroing-high-bits-in)).
```
0000000000000040 <onemask_even>:
  40:   89 f8                   mov    eax,edi
  42:   25 55 55 55 55          and    eax,0x55555555
  47:   29 c7                   sub    edi,eax
  49:   d1 ef                   shr    edi,1
  4b:   8d 04 47                lea    eax,[rdi+rax*2]
  4e:   c3                      ret    
4f: <end>
```
`0x4f - 0x40` = 14 bytes
This is [compiler output](http://gcc.godbolt.org/#compilers:!((compiler:g6,options:'-xc+-std%3Dgnu99+-O3+-Wno-all+-Wno-extra+-fno-verbose-asm',sourcez:PQKgBAIglg5lAuBDANmAKgJ0QVwLaIHIBnMAYTADcBTDIqAewDswRgAoAMwAoAPASgDeGKvGwZGvAGQA/AAwBVYAGYQAJj7BVAailzFSvmoDcAXzZtQYGPWQcqAEzKUadJmHocwPRvQzF3jFT4RADWYAAOAJ7wABZuHNiMAMbwDMysbDC8gsKi4mCqIDwAtCq8mvIyCsp8pmwx2UIiYsyFJWU8VfpqGqqmYGDAwGBEiNRgsVRg8uZsAMRQnrJsiXQwgY7ITDBgHABsACxcq7AbYFuMO/wCbAO5LWC6CgAyz8o9mmBaj516rzXGNhmNhQRjwXZrDZcUHg673cRPbrqTQ6X7VAzGMwndYOXb0ehgAC8YD0yiM8yojHsi1mlg4GHouC8Pj8JCisTciEYRAA7jQADRgABG2HB9HC4XodHgUxlGFwoJs9BgkQAXMKELIwFASFRqOl2NizkwgohQgB9XywY7c064/hgG4DI24vWU4JhYmkpTk522nGOehChAkYlcXhgAB8kbAAEY%2BGBJGA3YwPbVbmB4a0imBimAVEGQ%2BTgSt/cbAh7zZXJoxzUQYot4DbIfaE06wC7Ax6iST0SwCr6O2XcfRVD2I0nGWaQum/S3A8H4KH3GPo3HB1mCjm8wXF0Ri%2BZQLdmMTCgTvlRbsvClNvvRzMme8w82eBm/xzxE735G8DLcCWGz7Jr075hp%2BSZcH8NQADzQfGUaRrGtxXj2N5fI8jDFFQvTIWAKGniAXBAWeCZ3qhIDPgcIDnu45FAQWtwZBY4DwBgkQTDEUz0JMGBgDyiCRIKZpgIgERSnQQrIFMPAABx7CJRBMlyvI0CwhrDoGFbTuaKbNnajgOu2nbJt2XrooOxlTGBX6mqEs6Zs0%2BRUGo6Hhp%2BebYQhcbpsCCzJMg2D2FM0FEPA1JggAdDEkYrDChzmmKWkWimhzHHFBwJV4bYZtg6WZbZnrfr%2BFl5eCVk/DZaYbo5zDOWO3xubmwFefGB4WMMPBENx/hUDw4TCEQriMBFAwQPQVBEIwBDgjyvghBF7VgAA4o0m48KiXQAko0axnUQAA%3D)),filterAsm:(binary:!t,commentOnly:!t,directives:!t,intel:!t,labels:!t),version:3) from using xnor's excellent mask-once idea the opposite way. (And opposite terminology: the low bit is bit 0, which is even, not odd.)
```
unsigned onemask_even(unsigned x) {
  unsigned emask = ~0U/3;
  unsigned e = (x & emask);
  return e*2 + ((x - e) >> 1);
}
```
I didn't find any improvements over what the compiler does. I might have written it as `mov eax, 0x555...` / `and eax, edi`, but that's the same length.
---
The same function for 64bit integers takes 24 bytes (see the godbolt link). I don't see any way shorter than 10-byte `movabs rax, 0x55...` to generate the mask in a register. (x86's `div` instruction is clunky, so unsigned division of all-ones by 3 doesn't help.)
I did come up with a loop to generate the mask in rax, but it's 10 bytes (exactly the same length as the `mov imm64`).
```
# since 0x55 has its low bit set, shifting it out the top of RAX will set CF
0000000000000000 <swap_bitpairs64>:
   0:   31 c0                   xor    eax,eax      ; old garbage in rax could end the loop early
0000000000000002 <swap_bitpairs64.loop>:
   2:   48 c1 e0 08             shl    rax,0x8
   6:   b0 55                   mov    al,0x55      ; set the low byte
   8:   73 f8                   jnc    2 <swap_bitpairs64.loop>  ; loop until CF is set
000000000000000a <swap_bitpairs64.rest_of_function_as_normal>:
 # 10 bytes, same as   mov  rax, 0x5555555555555555
 # rax = 0x5555...
   a:   48 21 f8                and    rax,rdi
   ...
```
If we knew that none of the existing bytes in `rax` has their low bit set, we could skip the `xor`, and this would be 8 bytes long.
A previous version of this answer had a 10 byte loop using the [`loop`](http://www.felixcloutier.com/x86/LOOP:LOOPcc.html) insn, but it had a worst-case run-time of `0xFFFFFFFFFFFFFF08` iterations, because I only set `cl`.
[Answer]
# [Oasis](http://github.com/Adriandmen/Oasis), 17 bytes (non-competing)
```
n4÷axxn4÷xxe+3120
```
[Try it online!](http://oasis.tryitonline.net/#code=bjTDt2F4eG40w7d4eGUrMzEyMA&input=&args=NDc1MjU)
Oasis is a language designed by [Adnan](https://codegolf.stackexchange.com/users/34388/adnan) which is specialized in sequences.
Currently, this language can do recursion and closed form.
We use this formula: `a(4n+k) = 4a(n) + a(k), 0 <= k <= 3`
To specify the base case is simple: the `3120` at the end simply means that `a(0)=0, a(1)=2, a(2)=1, a(3)=3`.
```
n4÷axxn4÷xxe+3120
                0  a(0) = 0
               2   a(1) = 2
              1    a(2) = 1
             3     a(3) = 3
n                  push n (input)
 4÷                integer-divide by 4
   a               a(n/4)
    xx             double twice; multiply by 4
                   now we have 4a(n/4)
      n            push n (input)
       4÷xx        integer-divide by 4 and then multiply by 4
                   since there is no modulo currently, n%4
                   is built as n-(n/4*4)
           e       we should have done a(n-(n/4*4)), but this
                   is a shortcut for a(n-x) where x is the top
                   of stack. Therefore, we now have a(n-n/4*4)
                   which is a(n%4).
            +      add.
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), 10 bytes
```
BP2eP1ePXB
```
[Try it online!](http://matl.tryitonline.net/#code=QlAyZVAxZVBYQg&input=Ng)
[Modified version](http://matl.tryitonline.net/#code=OnEiQApCUDJlUDFlUFhC&input=MTA) to generate the first terms of the sequence ([OEIS A057300](https://oeis.org/A057300)).
### Explanation
```
B     % Take input implicitly. Convert to binary array
P     % Flip
2e    % Convert to two-row 2D array, padding with a trailing zero if needed. 
      % Because of the previous flip, this really corresponds to a leading zero
P     % Flip each column. This corresponds to swapping the bits
1e    % Reshape into a row
P     % Flip, to undo the initial flipping
XB    % Convert from binary array to number. Display implicitly
```
[Answer]
## zsh, 28 bytes
```
<<<$[`tr 12 21<<<$[[#4]$1]`]
```
Takes input as a command line argument, outputs on STDOUT.
It's not Bash-compatible because it uses zsh-specific base conversion syntax.
```
                       $1     input (first command line argument)
                 $[      ]    arithmetic expansion
                   [#4]       output in base 4
              <<<             pass the result of this to...
      tr                      the `tr' command
         12 21                and replace 1s with 2s, 2s with 1s
     `                    `   evaluate the result...
   $[                      ]  in another arithmetic expansion, to convert back
                                to base 10
<<<                           output the result on STDOUT
```
[Answer]
# Retina, 70 bytes
```
.+
$*
+`(1+)\1
$1x
x1
1
^
x
r`(.)(.)
$2$1
1
x@
+`@x
x@@
x*(@*)
$.1
0$
```
[Test suite.](http://retina.tryitonline.net/#code=JShgLisKJCoKK2AoMSspXDEKJDF4CngxCjEKXgp4CnJgKC4pKC4pCiQyJDEKMQp4QAorYEB4CnhAQAp4KihAKikKJC4xCjAkCg&input=MAoxCjkKODUKMjIw) (Slightly modified.)
### Well, just for fun: 7 bytes
```
T`12`21
```
Takes base-4 as input, and outputs as base-4.
[Try it online!](http://retina.tryitonline.net/#code=VGAxMmAyMQ&input=MzAxMg)
[Answer]
# 05AB1E, 8 bytes
```
4B12Â‡4ö
```
Thanks to @Adnan for -5 bytes!
**Uses CP-1252 encoding.**
[Try It Online!](http://05ab1e.tryitonline.net/#code=NEIxMsOC4oChNMO2&input=)
Explanation:
```
4B       - Take input and convert to base 4.
  12Â    - Push 12 bifurcated.
     ‡   - Transliterate [1, 2] to [2, 1].
      4ö - Convert to base 10.
```
[Answer]
# C, ~~32~~ ~~30~~ 29 bytes
```
f(x){return(x&~0U/3)*3+x>>1;}                // 30 bit version, see below
// less golfed:
f(x){return ((x & 0x55555555)*3 + x) >>1;}   //  >> is lower precedence than +
```
Algorithm [copied from xsot's comment on xnor's Python answer](https://codegolf.stackexchange.com/a/81226/30206). Instead of masking both ways, mask one way and combine.
This [compiles to the same asm as the last version I tested](http://gcc.godbolt.org/#compilers:!((compiler:g6,options:'-xc+-std%3Dgnu99+-O3+-Wno-all+-Wno-extra+-fno-verbose-asm',sourcez:PQKgBAIglg5lAuBDANmAKgJ0QVwLaIHIBnMAYTADcBTDIqAewDswRgAoAMwAoAPASgDeGKvGwZGvAGQA/AAwBVYAGYQAJj7BVAailzFSvmoDcAXzZtQYGPWQcqAEzKUadJmHocwPRvQzF3jFT4RADWYAAOAJ7wABZuHNiMAMbwDMysbDC8gsKi4mCqIDwAtCq8mvIyCsp8pmwx2UIiYsyFJWU8VfpqGqqmYGDAwGBEiNRgsVRg8uZDXkT08P5RsW5UPOHCRK6MAHRsAOKNuS1eOp16NSBKAHw3AIx1ABLHzeK8511XBpr9g8N3MBQEjIegAdxoEWESQcVGSU1iiGYWgANCN6BMYsCgSQwTFEPAwAccWB7PQoIwYGwAJKvPISC7VAzXLQ8O6PEwDf5gJJiYSMQnUWhpABcI0QuCmiCIuDA0qJ2TljEcL34YDxUCSMXVNARVCI8AcbAAUnSWlxdEzDEpWb06mwAMRQTyyNiJOgwQKOUGUsAcABsABYuO7YF6wD6YF5BGwBid8pb5AAZJPKHqaMBaMCJlNXPpsMxsCmEjger1cYvRpr0xNp9SaT6XZl9Myhz0OP30DEAXjATaMjrh9md5gs4A4GHosu8vmW0TizCRRAhGDRACNsIT6OFwvQ6IaJjRcBSbPQYJExWuELISVRqOl2G3w0wgtKQgB9XywEOMMsdtUCLGYBPh2d5wsEYS9v2QEgY49BXvAJC9haPBgIC9x8GAkhgGBjAQbUQHxq0RRgMUYAqPBCBEAOhZur%2BYYdi%2BEHvsxkyMO%2BRBYhw8A/n%2BjgATB9HtnBEFgFBTIsAUA4DLB7iqGJ2aodhU5vgRMlCc%2BCFIXJaE3GAjyEW8xGoWRFFaTRswgLGzC9oUGJZlQsbaYUUxZvQ5g4QpzBkXZXJcshSl9goqYGLGPbZt5OG9H5AVYdmeghXwAA8SUYbp9yxk5CkuZmEXFFQvSZWAWW2SAXCRXZmFudlIDeYGID2e4NWRRRsYZGOEwYJEmJTIsMSQmCiCRGi8qIBEe50GuyBTDwAAc/pyjKSrLpCGSyUxb7vrhvEMfxmGAepfE4aJ4n6NJwEaaBCm8HFr6hGpYBEThai5ShpFRbp%2BkEYWTrJMg2D2FMSUGsOAq7DENxusWQbvlugTMbhQYhtDgaw9GYAHcBKNo3dkFBcmyjndg2OElMsXYbjD1PVQL1Zm9ZEFZ9GEWUAAA%3D)),filterAsm:(binary:!t,commentOnly:!t,directives:!t,intel:!t,labels:!t),version:3), and it works (for x up to `0x3FFFFFFF`, and for x above that as long as bit 30 isn't set, see below). In machine code, it's the same length as my existing asm answer.
---
**The above version always clears the high bit of the result**. The best safe version is 32 bytes:
```
g(x){return 2*x-3*(x/2U&~0U/3);}   // safe 32bit version, works for all x
```
The Python version doesn't have this problem, because [python uses arbitrary-precision integer types when needed](https://docs.python.org/2/library/stdtypes.html), instead of truncating to a fixed upper limit.
[Answer]
# Javascript (ES6), ~~113~~ 109 bytes
Saved 4 bytes thanks to [Upgoat](https://codegolf.stackexchange.com/users/40695/upgoat)
```
n=>+('0b'+/(..)+$/.exec('0'+n.toString`2`)[0].split``.reduce((p,c)=>p.length-1?[p.join(c)]:[p[0],c],[''])[0],2)
```
### How it works
```
n=>+('0b'+                              //parse as binary literal
    /(..)+$/.exec('0'+n.toString`2`)[0] //convert to binary string with an even number of digits
        .split``                        //convert to array
        .reduce((p,c)=>p.length-1?[p.join(c)]:[p[0],c],[''])
                                        //swap all numbers
)
```
[Answer]
# J, 20 bytes
```
4#.0 2 1 3{~4#.^:_1]
```
### Usage
```
>> f =: 4#.0 2 1 3{~4#.^:_1]
>> f 85
<< 170
```
Where `>>` is STDIN and `<<` is STDOUT.
### Ungolfed
```
to_base   =: 4 #.^:_1 ]
transpose =: 0 2 1 3 {~ to_base
from_base =: 4 #. transpose
```
Three forks.
### Sidenote
In the official interpreter, `^:_1` can be replaced by `inv`, saving 1 byte.
However, none of the online interpreters implement this.
[Answer]
# C#, 44 bytes
Based on the [C implementation](https://codegolf.stackexchange.com/a/81228/54201)
`int f(int n)=>n>3?4*f(n/4)+f(n%4):n%2*2+n/2;`
Try it here: [C# pad](http://csharppad.com/gist/6a4c768f58bf8f4bc2e21c93c34920d4)
[Answer]
# [INTERCAL](http://www.catb.org/~esr/intercal/), 60 bytes
```
DOWRITEIN.1PLEASE.1<-!1~#21845'$.1~#43690DOREADOUT.1DOGIVEUP
```
[Try it online!](https://tio.run/##y8wrSS1KTsz5/9/FPzzIM8TV00/PMMDH1THYVc/QRlfRsE7ZyNDCxFRdRQ/INDE2szRw8Q9ydXTxDw3RM3Txd/cMcw0N@P/fzT80SCHYNczVT8ENKKQQEu4PZnABAA "INTERCAL – Try It Online")
Works for 16-bit integers, with I/O done in the most natural format for INTERCAL: the input is a series of decimal digits spelled out in one of several natural or constructed languages, and the output is in "butchered Roman numerals".
This is one of those rare challenges where INTERCAL's binary operators can actually be used at all intuitively, since repositioning bits is what they're all about. Select (`~`) takes the bits from its first argument corresponding to ones in its second argument and pads them to the right with zeroes, and mingle (`$`) interleaves the bits from its arguments such that the bits from the first argument are more significant. So the straightforward solution is to select out the less significant alternating bits (`.1~#21845`), select out the more significant alternating bits (`.1~#43690`), and interleave them back together in the opposite order. Fortunately for byte count, although INTERCAL's operators have no defined precedence (as the language's goal is to have no precedents), it turns out that the C-INTERCAL on TIO doesn't require much grouping for this particular expression, costing only one byte since `'.` can be abbreviated `!`.
With support for 32-bit integers:
# [INTERCAL](http://www.catb.org/~esr/intercal/), 67 bytes
```
DOWRITEIN:1PLEASE:1<-':1~#0$#65535'$:1~#65535$#0DOREADOUT:1DOGIVEUP
```
[Try it online!](https://tio.run/##y8wrSS1KTsz5/9/FPzzIM8TV08/KMMDH1THY1crQRlfdyrBO2UBF2czU1NhUXQXEAzNVlA1c/INcHV38Q0OsDF383T3DXEMD/v938w8NUgh2DXP1U3ADCimEhPuDGVwA "INTERCAL – Try It Online")
INTERCAL doesn't allow 32-bit literals, which actually makes this a bit *easier* to read, since it means the magic constants for selecting alternating bits have to be constructed by mingling two 16-bit literals together, where one's all zeroes and the other's all ones. (Actually, even if there were 32-bit literals, this would still be shorter. `#0$#65535` is two bytes off `#1431655765`, and the same goes for the other one.) This communicates the whole process unnaturally well for INTERCAL.
An alternate approach with clumsy use of *operand overloading*:
# [INTERCAL](http://www.catb.org/~esr/intercal/), 71 bytes
```
DO:1<-:1/.2$.3PLEASEWRITEIN:2DO:1<-:2PLEASE:2<-.3$.2DOREADOUT:2DOGIVEUP
```
[Try it online!](https://tio.run/##y8wrSS1KTsz5/9/F38rQRtfKUF/PSEXPOMDH1THYNTzIM8TV08/KCCppBBG2MrLR1TNW0QMKB7k6uviHhoBUuHuGuYYG/P/v5h8apBDsGubqp@AGFFIICfcHM7gA "INTERCAL – Try It Online")
This does away with selecting altogether by declaring that `:1` is `.2` mingled with `.3`, setting `:1` to the input, then outputting `.3` mingled with `.2`. Since `:1` was overloaded as `.2$.3`, `DO :1 <- :2` assigns values to `.2` and `.3` such that `:1` acquires the value of `:2`, which results in `.2` containing the more significant alternating bits from `:2` and `.3` containing the less significant alternating bits. This would be the shorter of the two 32-bit solutions by four bytes if `PLEASE WRITE IN :1` could replace `PLEASE WRITE IN :2 DO :1 <- :2` with overloaded `:1`, but `CALCULATING` turns out to be necessary for using overloading. I also feel like there might be some shorter way to carry out the overloading itself than starting the program with `DO:1<-:1/.2$.3`, but since this is INTERCAL I also also feel like there can't be.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 8 bytes
```
ḋṁo↔‰2B4
```
[Try it online!](https://tio.run/##yygtzv7//@GO7oc7G/MftU151LDByMnk////JuamRqYA "Husk – Try It Online")
Same as the Jelly solution.
# [Husk](https://github.com/barbuz/Husk), ~~9~~ 8 bytes
```
ḋṁ↔C_2Θḋ
```
[Try it online!](https://tio.run/##yygtzv7//@GO7oc7Gx@1TXGON7IyAPL@//9vYm5qZAoA "Husk – Try It Online")
General method.
-1 byte from Jo King. (using `Θ`)
[Answer]
## Mathematica, 44 bytes
```
Fold[3#+##&,#~IntegerDigits~4/.{1->2,2->1}]&
```
Same approach as my CJam answer: convert to base-4, swap 1s and 2s, convert back. It also uses alephalpha's trick to replace `FromDigits` with a `Fold` operation to save one byte.
[Answer]
## Actually, 16 bytes
```
4@¡"21""12"(t4@¿
```
[Try it online!](http://actually.tryitonline.net/#code=NEDCoSIyMSIiMTIiKHQ0QMK_&input=MjIw)
Explanation:
```
4@¡"21""12"(t4@¿
4@¡               base 4 representation of n
   "21""12"(t     translate (swap 1s and 2s)
             4@¿  base 4 to decimal
```
[Answer]
# J, 22 bytes
```
([:,_2|.\,&0)&.(|.@#:)
```
Alternative approach based on array manipulation instead of the base 4 trick.
## Usage
```
   f =: ([:,_2|.\,&0)&.(|.@#:)
   (,.f"0) 0 1 9 85 220 1827 47525
    0     0
    1     2
    9     6
   85   170
  220   236
 1827  2835
47525 30298
```
## Explanation
```
([:,_2|.\,&0)&.(|.@#:)  Input: n
                   #:   Get the value as a list of base 2 digits
                |.@     Reverse it
(           )&.         Apply to the list of base 2 digits
         ,&0            Append a zero to the end of the list
    _2  \               Split the list into nonoverlapping sublists of size 2
      |.                Reverse each sublist
 [:,                    Flatten the list of sublists into a list
             &.(    )   Apply the inverse of (reversed base 2 digits)
                        to convert back to a number and return it
```
[Answer]
## REXX, 88 bytes
```
n=x2b(d2x(arg(1)))
o=0
do while n>''
  parse var n a+1 b+1 n
  o=o||b||a
  end
say x2d(b2x(o))
```
[Answer]
## x86 machine code - 19 bytes
Callable function, input in `ebx` output in `eax`.
Lisitng :
```
    16                                  global tukar_bits
    17                                  tukar_bits:
    18                                  
    19 00000016 89DA                    mov  edx, ebx
    20 00000018 8D0412                  lea  eax, [edx + edx]
    21 0000001B D1EA                    shr  edx, 1
    22 0000001D 81E255555555            and  edx, 0x55555555
    23 00000023 8D1452                  lea  edx, [edx + edx * 2]
    24 00000026 29D0                    sub  eax, edx
    25 00000028 C3                      ret
```
[Try it online!](https://tio.run/##XY/RbsMgDEWfw1dYlfaydqirFm3qr3RRZQLJ0AipwJ3o12eEspDODwhfXx/b6L0ahLm99G07cW1JmbO/WcIAdrw41enAuAqknIWLi/WOcYmErBvoCNxT1HrYPMlPu2GconOxI42a8d6MwsCA2rL5OTKIMYw/4KTewcl5vf1oqqS2aEzqgqpabOBE2IHDsPLQ9RvdWWjyf06jEDIxLtbMEqwQXicEwP8CRva@gO8XPkxPDrgrTtGUDyorsPItx4GSsU@JULZTM@kUddjO1SZV/JfL3teUo5U534c6xwohHxDwDIeMuYo8IMrLqmya3t7rQ/0L), using x86-64 caller.
---
## x86 machine code - 21 bytes
Callable function, input in `ebx`output in `eax`.
Listing :
```
     1                                  global tukar_bits
     2                                  tukar_bits:
     3                                  
     4 00000000 89D8                    mov  eax, ebx
     5 00000002 89C2                    mov  edx, eax
     6 00000004 2555555555              and  eax, 0x55555555
     7 00000009 D1EA                    shr  edx, 1
     8 0000000B 81E255555555            and  edx, 0x55555555
     9 00000011 8D0442                  lea  eax, [edx + eax * 2]
    10 00000014 C3                      ret
```
[Try it online!](https://tio.run/##ZY/RbsMgDEWfw1dYlfaydqirFm3qr7RR5QSSoRJSgVfRr8@AsCTb/ICs6@OLLzon@1o/XrqmGbkyJPXFPQyhBzPcrGyVZ1x6ktbAzYZ5y7hAQtb2dATuKGgdbJ7E2WwYp0DOONKgGO/0UGvoURkWnyODUP1wByvUDk7Wqe1HVSS1Qa3TFhTFjIGt/Q4s@hVDX1e0l1qR@yG1RMiO4bAqSrCycCpZAPwdYPDeL8ZTwl@/JwImxUoac6DlBLa0SziQcU/WfqWIqOCkoBGZ2fsyVxq4T5vR1xUo/oMpcXI4hTFsYw/PcKjmS9k4vr2Xh/Ib), using x86-64 caller.
]