Looking for a generalised or non-iterative maths equation for the process of slicing up a range value

16 Views Asked by At

Suppose I have a integer range $[0,n]$ and an integer $x$ within that range.

Suppose I can generate an integer $y$ though this iterative process:

  1. $x$ is converted to Base 2
  2. For each bit from most to least significant:
    • If 0 then cut the current number range in half, keeping the upper half as the new range.
    • If 1 then cut the current number range in half, keeping the lower half as the new range.
  3. Once there are no more bits, $y$ is the current value of $n$.

Is there a maths equation that can solve for $y$ without this iteration?

Indeed, is there any generalised equation or known algorithm for this behaviour?

N.B. Feel free to change the question title if you know the type of equation or algorithm this is.