Find value of 2 variables from 1 number If we have a formula
$2^x+2^y=z$
And we know x and y can only be 0, 1, 2, 3, 4, or 5, and we know z, what is a formula we could use to find x and y?
 A: If $\log_2z$ is an integer, then $x=y=\log_2z -1$.
Otherwise, we assume $x<y$.  In this case $y=\lfloor \log_2z\rfloor$ and $x=\log_2(z-2^y)$.
$\log_2$ indicates logarithm to base 2 and $\lfloor\cdot \rfloor$ indicates the floor function.
A: How about this:
$$x=\lfloor \log_2 z \rfloor$$
$$y=\log_2 (z-2^x)$$
Of course, if $z=2^x+0$, this pair of formulae will fail.
A: You mean that $z$ is some number you want to represent as a 6 digit binary number, say
$z=100100_2$ and you want to find what digits contain ones?
To do you this, you repeatedly divide by two and record the remainder. For example, if $z=30$, you would get: $011110$. As you can see, there is no guarantee that you only have two digits that arre equal to one.
A: There's an easy algorithm to do this. For the sake of argument, say $x > y > 0$.
$$2^x + 2^y = z \implies 2^y (2^{x-y} + 1) = z$$
$2^y$ is even. $2^{x-y} + 1$ is odd. So, factor out as many powers of 2 as you can. That is $2^y$. What's left over is $2^{x-y} + 1$; subtract 1, and see if it's a power of 2.
It's easy to tell if $y = 0$. Your sum will be odd. If $x = y$, then your sum is a power of 2. If $y > x$, just switch them. That's all the cases!
