Generate ordered binary combinations without repetitions Challenge

Write the shortest program that receives two signed integers n and i and for each i between 1 and 2^n - 1 returns the next ordered permutation based on the binary representation of the number.  There is no specific order of the combinations but the number of 1s in the binary representation must always stay the same or grow and numbers each output may happen only once 

Clarification

The goal of this program is to generate all the combinations without repetitions of a set. To do so you may consider that each item is represented by a bit in a bit mask, that way if you have three items, A, B and C represented by 001, 010 and 100 respectively the combinations ABC, ACB, BCA, BAC, CAB and CBA are all represented as 111.

For increasing values of i your program should output a new combinations always with the same number of elements or more.

Input

You may read the input in the format 


  n i


or just use n and i variables, whichever suits you best.

Output

You may output a single number k

Sample cases

Each test case is two lines, input followed by output with a binary representation here for demonstration purposes:

3 1
1 (001)

3 2
4 (100)

3 3
2 (010)

3 4
6 (110)

3 7
7 (111)


Rules


Shortest code wins (bytes)
The result can be returned by a function or printed by your program.
You may assume n and i variables already exist, there's no need to handle input if you don't want to.
The answers with the same number of 1 bits can be output in any order
The input is guaranteed to be well formed and 1 &lt;= i &lt;= 2^n - 1.


Happy Golfing!
Ruby, 72 bytes

p [0,1].repeated_permutation(n).sort_by{|x|x.count 1}[i].join('').to_i 2


How it works:

irb(main):001:0&gt; [0, 1].repeated_permutation(3).to_a  # generate permutations
=&gt; [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1],
    [1, 1, 0], [1, 1, 1]]

irb(main):002:0&gt; _.sort_by {|x| x.count 1 }           # sort by number of 1's
=&gt; [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 1], [1, 0, 1],
    [1, 1, 0], [1, 1, 1]]

irb(main):003:0&gt; _[2].join ''  # convert array of binary digits to string
=&gt; "010"
irb(main):004:0&gt; _.to_i 2      # interpret string as binary number
=&gt; 2