code: function (i, p) {
    var k = Math.floor(Math.log(i) / Math.log(2))
      , r = i - Math.pow(2, k);
    if (r === 0) {
      return Math.pow(2, p - k);
    }
    var nr = (i - 2*r).toString(2).split('').reverse().join('');
    return (parseInt(nr, 2) << p - nr.length) + Math.pow(2, p - k - 1);
  }
docs: helpers to initialize duel tournaments http://clux.x-pec.com/entries/view/2407