# 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?

-
With so few possibilities, why not just try them all? –  vadim123 Apr 26 '13 at 1:16
The point is to get a formula, there could just be a little sheet but I want a formula. –  Cool12309 Apr 26 '13 at 1:17

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.

-

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.

-
This is what I wanted. Thanks! –  Cool12309 Apr 26 '13 at 1:32
@Cool12309: If you regard a power of two $z$ as $z=2^x+2^y$ with $x=y$ you can say $x=\lceil \log_2 z \rceil-1$ and use your formula for $y$ –  Ross Millikan Apr 26 '13 at 2:52

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.

-
Yea, I didn't mean the 8-bit thing. x and y can only be 0 through 5 and z can be any number 2-64 –  Cool12309 Apr 26 '13 at 1:23
He's saying the "8 bit thing" is exactly the same problem you're trying to solve. –  Henry Swanson Apr 26 '13 at 1:30
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!