[Question]
  [
So you are given a **POSITIVE** base 10 (decimal) number. Your job is to reverse the binary digits and return that base 10 number. 
### Examples:
```
1 => 1 (1 => 1)
2 => 1 (10 => 01)
3 => 3 (11 => 11)
4 => 1 (100 => 001)
5 => 5 (101 => 101)
6 => 3 (110 => 011)
7 => 7 (111 => 111)
8 => 1 (1000 => 0001)
9 => 9 (1001 => 1001)
10 => 5 (1010 => 0101)
```
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") challenge, so the solution that uses the least bytes wins.
This is [A030101](http://oeis.org/A030101) in the OEIS.
  
[Answer]
# [Python](https://docs.python.org/3/), 29 bytes
```
lambda n:int(bin(n)[:1:-1],2)
```
[**Try it online!**](https://tio.run/nexus/python2#K1KwVYj5n5OYm5SSqJBnlZlXopGUmaeRpxltZWilaxirY6T5v6AIJFykYWiiqfkfAA)
This is an anonymous, unnamed function which returns the result. 
---
First, `bin(n)` converts the argument to a binary string. We would ordinarily reverse this with the slice notation `[::-1]`. This reads the string with a step of **-1**, i.e. backwards. However, binary strings in Python are prefixed with an `0b`, and therefore we give the slicing's second argument as **1**, telling Python to read backwards terminating at index **1**, thus not reading indexes **1** and **0**.
Now that we have the backwards binary string, we pass it to `int(...)` with the second argument as **2**. This reads the string as a base 2 integer, which is then implicity returned by the lambda expression.
[Answer]
# Python, 29 bytes
```
lambda n:int(bin(n)[:1:-1],2)
```
[**Try it online**](https://tio.run/nexus/python2#@59mm5OYm5SSqJBnlZlXopGUmaeRpxltZWilaxirY6T5v6AIKKyQpmFooPkfAA)
[Answer]
## JavaScript (ES6), ~~30~~ 28 bytes
*Saved 2 bytes thanks to @Arnauld*
```
f=(n,q)=>n?f(n>>1,q*2|n%2):q
```
This basically calculates the reverse one bit at a time: We start with **q = 0**; while **n** is positive, we multiply **q** by 2, sever the last bit off of **n** with `n>>1`, and add it to **q** with `|n%2`. When **n** reaches 0, the number has been successfully reversed, and we return **q**.
Thanks to JS's long built-in names, solving this challenge the easy way takes 44 bytes:
```
n=>+[...n.toString(2),'0b'].reverse().join``
```
Using recursion and a string, you can get a 32 byte solution that does the same thing:
```
f=(n,q='0b')=>n?f(n>>1,q+n%2):+q
```
[Answer]
# Java 8, ~~53~~ ~~47~~ ~~46~~ 45 bytes
* *-4 bytes thanks to Titus*
* *-1 byte thanks to Kevin Cruijssen*
This is a lambda expression which has the same principle as [ETH's answer](https://codegolf.stackexchange.com/a/105633/60919) (although recursion would have been too verbose in Java, so we loop instead):
```
x->{int t=0;for(;x>0;x/=2)t+=t+x%2;return t;}
```
[**Try it online!**](https://repl.it/FAsU/9) 
This can be assigned with `IntFunction<Integer> f = ...`, and then called with `f.apply(num)`. Expanded, ungolfed and commented, it looks like this:
```
x -> { 
    int t = 0;           // Initialize result holder   
    while (x > 0) {      // While there are bits left in input:
        t <<= 1;         //   Add a 0 bit at the end of result
        t += x%2;        //   Set it to the last bit of x
        x >>= 1;         //   Hack off the last bit of x
    }              
    return t;            // Return the final result
};
```
[Answer]
# J, 6 bytes
```
|.&.#:
```
`|.` reverse
`&.` under
`#:` base 2
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 3 bytes
```
BU·∏Ñ
```
[Try it online!](https://tio.run/nexus/jelly#@@8U@nBHy////w0NAA "Jelly ‚Äì TIO Nexus")
```
B   # convert to binary
 U  # reverse
  ·∏Ñ # convert to decimal
```
[Answer]
## Mathematica, 19 bytes
```
#~IntegerReverse~2&
```
[Answer]
# C, ~~48~~ ~~44~~ ~~43~~ 42 bytes
-1 byte thanks to gurka and -1 byte thanks to anatolyg:
```
r;f(n){for(r=n&1;n/=2;r+=r+n%2);return r;}
```
Previous 44 bytes solution:
```
r;f(n){r=n&1;while(n/=2)r=2*r+n%2;return r;}
```
Previous 48 bytes solution:
```
r;f(n){r=0;while(n)r=2*(r+n%2),n/=2;return r/2;}
```
Ungolfed and usage:
```
r;
f(n){
 for(
  r=n&1;
  n/=2;
  r+=r+n%2
 );
 return r;}
}
main() {
#define P(x) printf("%d %d\n",x,f(x))
P(1);
P(2);
P(3);
P(4);
P(5);
P(6);
P(7);
P(8);
P(9);
P(10);
}
```
[Answer]
# Labyrinth, 23 bytes
```
?_";:_2
  :   %
 @/2_"!
```
Well, this is awkward... this returns the reverse BINARY number...
Thanks @Martin Ender for pointing out both my bug and my ID 10T error.
So this doesn't work, I'll have to find another solution.
[Answer]
# Ruby, ~~29~~ 28 bytes
```
->n{("%b"%n).reverse.to_i 2}
```
"%b" % n formats the input n as a binary string, reverse, then convert back to a number
Usage/Test cases:
```
m=->n{("%b"%n).reverse.to_i 2}
m[1] #=> 1
m[2] #=> 1
m[3] #=> 3
m[4] #=> 1
m[5] #=> 5
m[6] #=> 3
m[7] #=> 7
m[8] #=> 1
m[9] #=> 9
m[10] #=> 5
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 3 bytes
```
bRC
```
[Try it online!](https://tio.run/nexus/05ab1e#@58U5Pz/v6EBAA "05AB1E ‚Äì TIO Nexus")
[Answer]
# [Java (OpenJDK)](http://openjdk.java.net/), 63 bytes
```
a->a.valueOf(new StringBuffer(a.toString(a,2)).reverse()+"",2);
```
[Try it online!](https://tio.run/nexus/java-openjdk#TY49C8IwFEXn9Fc8OiWoQV2LDm6C4uBYHJ71pQTatOSjKqW/vaZUxPW8e899bbhXuoCiQufgjNpAnzDn0UeojSersCA4YX1/IPQTAhsMP8ZTSRZeIoMhS1g7a77FrtEPqKOMX73VpsxvgLZ0YnKzr0vtRlztUXZYBboobugJc/oQlCLLUfpmBhyXWyGkpY6sIy4WaRpBNjIWl9n17TzVsgletjHtK8OV/PvxtzC9kK9vQohYG5JhHDfrDw "Java (OpenJDK) ‚Äì TIO Nexus")
Thanks to poke for -12 bytes and to Cyoce for -8 bytes!
[Answer]
# [Perl 6](http://perl6.org/), 19 bytes
```
{:2(.base(2).flip)}
```
[Answer]
# Haskell, 36 bytes
```
0!b=b
a!b=div a 2!(b+b+mod a 2)
(!0)
```
Same algorithm (and length!) as ETHproductions‚Äô [JavaScript](https://codegolf.stackexchange.com/a/105633/3852) answer.
[Answer]
# [Bash](https://www.gnu.org/software/bash/)/Unix utilities, ~~24~~ 23 bytes
```
dc -e2i`dc -e2o?p|rev`p
```
[Try it online!](https://tio.run/nexus/bash#@5@SrKCbapSZAKHz7QtqilLLEgr@/zc0@A8A "Bash ‚Äì TIO Nexus")
[Answer]
# PHP, 33 bytes
```
<?=bindec(strrev(decbin($argn)));
```
convert to base2, reverse string, convert to decimal. Save to file and run as pipe with `-F`.
**no builtins:**
**iterative, 41 bytes**
```
for(;$n=&$argn;$n>>=1)$r+=$r+$n%2;echo$r;
```
While input has set bits, pop a bit from input and push it to output. Run as pipe with `-nR`.
**recursive, 52 bytes**
```
function r($n,$r=0){return$n?r($n>>1,$r*2+$n%2):$r;}
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~5~~ 3 bytes
-2 bytes thanks to Shaggy
```
¬¢√î√ç
```
[Try it online!](https://tio.run/##y0osKPn//9Ciw1MO9/7/b2gAAA "Japt ‚Äì Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 4 bytes
```
BPXB
```
[Try it online!](https://tio.run/nexus/matl#@@8UEOH0/78ZAA "MATL ‚Äì TIO Nexus")
### Explanation
```
B     % Input a number implicitly. Convert to binary array
P     % Reverse array
XB    % Convert from binary array to number. Display implicitly
```
[Answer]
# Pyth, 6 bytes
```
i_.BQ2
```
[Test suite available here.](http://pyth.herokuapp.com/?code=i_.BQ2&input=2&test_suite=1&test_suite_input=1%0A2%0A3%0A4%0A5%0A6%0A7%0A8%0A9%0A10&debug=0)
### Explanation
```
i_.BQ2
    Q     eval(input())
  .B      convert to binary
 _        reverse
i    2    convert from base 2 to base 10
```
[Answer]
# Scala, 40 bytes
```
i=>BigInt(BigInt(i)toString 2 reverse,2)
```
### Usage:
```
val f:(Int=>Any)=i=>BigInt(BigInt(i)toString 2 reverse,2)
f(10) //returns 5
```
### Explanation:
```
i =>          // create an anonymous function with a parameter i
  BigInt(       //return a BigInt contructed from
    BigInt(i)     //i converted to a BigInt
    toString 2    //converted to a binary string
    reverse       //revered
    ,           
    2             //interpreted as a binary string
  )
```
[Answer]
# [APL (Dyalog Unicode)](http://www.dyalog.com/), 10 [bytes](//codegolf.meta.stackexchange.com/a/9429/74163)
```
2‚ä•‚àò‚äñ2‚àò‚ä•‚ç£¬Ø1
```
[Try it!](http://tryapl.org/?a=f%u21902%u22A5%u2218%u22962%u2218%u22A5%u2363%AF1&run)
Thanks to @Ad√°m for 2 bytes.
### How it works:
This is a tacit function. To use it, it has to be assigned a name (like `f‚Üê`) and then called over an input; for the test cases: `f 1`, `f 2`, etc.
```
2‚ä•‚àò‚äñ2‚àò‚ä•‚ç£¬Ø1  # Main function; tacit.
       ‚ç£¬Ø1  # invert
    2‚àò‚ä•     # convert from base 2 (which is inverted to 'convert to base 2')
  ‚àò‚äñ        # flip, then
2‚ä•          # convert from base 2 
```
[Answer]
# Mathematica, 38 bytes
```
#+##&~Fold~Reverse[#~IntegerDigits~2]&
```
[Answer]
# Groovy, 46 bytes
```
{0.parseInt(0.toBinaryString(it).reverse(),2)}
```
[Answer]
# [CJam](https://sourceforge.net/p/cjam), 8 bytes
```
ri2bW%2b
```
[Try it online!](https://tio.run/nexus/cjam#@1@UaZQUrmqU9P@/GQA "CJam ‚Äì TIO Nexus")
### Explanation
```
ri          e# Read integer
  2b        e# Convert to binary array
    W%      e# Reverse array
      2b    e# Convert from binary array to number. Implicitly display
```
[Answer]
## Batch, 62 bytes
```
@set/an=%1/2,r=%2+%1%%2
@if %n% gtr 0 %0 %n% %r%*2
@echo %r%
```
Explanation: On the first pass, `%1` contains the input parameter while `%2` is empty. We therefore evaluate `n` as half of `%1` and `r` as `+%1` modulo 2 (the `%` operator has to be doubled to quote it). If `n` is not zero, we then call ourselves tail recursively passing in `n` and an expression that gets evaluated on the next pass effectively doubling `r` each time.
[Answer]
# C#, 98 bytes
```
using System.Linq;using b=System.Convert;a=>b.ToInt64(string.Concat(b.ToString(a,2).Reverse()),2);
```
[Answer]
## R, 55 bytes
```
sum(2^((length(y<-rev(miscFuncs::bin(scan()))):1)-1)*y)
```
Reads input from stdin and consequently uses the `bin` function from the `miscFuncs` package to convert from decimal to a binary vector. 
[Answer]
# [Pushy](https://github.com/FTcode/Pushy), 19 bytes
No builtin base conversion!
```
$&2%v2/;FL:vK2*;OS#
```
[**Try it online!**](https://tio.run/##Kygtzqj8/19FzUi1zEjf2s3HqszbSMvaP1j5////hgYA "Pushy ‚Äì Try It Online")
Pushy has two stacks, and this answer makes use of this extensively.
There are two parts two this program. First, `$&2%v2/;F`, converts the number to its reverse binary representation:
```
            \ Implicit: Input is an integer on main stack.
$      ;    \ While i != 0:
 &2%v       \   Put i % 2 on auxiliary stack
     2/     \   i = i // 2 (integer division)
        F   \ Swap stacks (so result is on main stack)
```
Given the example 10, the stacks would appear as following on each iteration:
```
1: [10]
2: []
1: [5]
2: [0]
1: [2]
2: [0, 1]
1: [1]
2: [0, 1, 0]
1: [0]
2: [0, 1, 0, 1]
```
We can see that after the final iteration, `0, 1, 0, 1` has been created on the second stack - the reverse binary digits of 10, `0b1010`.
The second part of the code, `L:vK2*;OS#`, is taken from my previous answer which converts [binary to decimal](https://codegolf.stackexchange.com/a/102269/60919). Using the method decsribed and explained in that answer, it converts the binary digits on the stack into a base 10 integer, and prints the result.
[Answer]
## [Labyrinth](https://github.com/m-ender/labyrinth), 22 bytes
```
_2/?+:{!
}    "
++:{%#
```
[Try it online!](https://tio.run/##y0lMqizKzCvJ@P8/3kjfXtuqWpGrVgEIlLi0gRxV5f//DQ0MAA "Labyrinth ‚Äì Try It Online")
### Explanation
The idea is to disassemble the input bit-by-bit while assembling the output from the other end. Luckily, it's easiest to deconstruct a number from the right, and build one up from the left, so this automatically reverses the bits. We'll generally be keeping what's left of the input on top the main stack, and what we've already computed of the result on top of the auxiliary stack.
```
_2/  When the program starts out, this doesn't really do anything, because it
     just divides an implicit zero by 2.
?+   Read an integer N from STDIN and add it to the implicit zero on top of 
     the stack.
     The main loop starts here:
:    Duplicate what's left of N. This is also used as the conditional to end
     the loop. Once this reaches zero, the IP moves straight ahead, exiting
     the loop. Otherwise, the IP turns south which continues the loop.
#    Push the stack depth, 2.
%    Take N modulo 2, i.e. get its least significant bit.
{    Fetch the intermediate result R from the auxiliary stack.
:+   Double it (shifting its existing bits left by one position).
+    Add the bit we just extracted from N to R.
}    Put R back on the auxiliary stack.
_2/  Divide N by 2, dropping its least significant bit.
?+   We're at EOF, so ? just pushes zero and the + gets rid of that zero.
     Then the main loop starts over.
     When we exit the loop (once N is zero), this bit is run:
{    Retrieve R from the auxiliary stack.
!    Print it.
     The IP hits a dead end and turns around.
{:+? Various shenanigans which simply leave a zero on top of the stack.
/    Terminates the program due to the attempted division by zero.
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 3 bytes
```
b·πòB
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJQQSIsIiIsImLhuZhCIiwiIiwiIl0=)
Converts to a binary string, reverses it, then puts it back in base 10.
]