# Can I turn 2 numbers to one number?? [closed]

I have two different numbers A and B, each one range from 0 to 255, I want to do some math and add the data of the two numbers to one number C, then if need the numbers A and B back again I can convert C back to A and B.

Can this be done? Or Is it even possible?? I don't mind about boring calculations as this will be an automated process.

• This definitely can't be done. Suppose such a process were possible, then spit C in half and keep repeating it. Lossless compression!
– user208649
Apr 11, 2016 at 6:53
• It sounded like he wanted to take $A+B =C$ back to $A$ and $B$, but I suppose loosely interpreting 'add' makes it doable.
– user208649
Apr 11, 2016 at 7:00
• Hint: C= (A << 8) + B;
– user65203
Apr 11, 2016 at 7:03
• bitmasks, my friend. While mathematical, this seems to be a better question for Stack overflow, anyways. Voting to close. Apr 11, 2016 at 7:04
• @jaska: it can stay on Mathematics if the numbers are allowed to be real. ;-)
– user65203
Apr 11, 2016 at 7:10

The standard way of doing this is just putting one number after the other, in binary. The new number will range from $0$ to $65535$.

The concrete operation is $c=256a+b$ (or c = a << 8 + b if you're into bitshifts`), and you recover $a$ and $b$ by $a=c/256$ (integer division, i.e. rounded down) and $b=c\%256 = c-256 a$.

$A$ and $B$ both hold $8$ bits of information, and each can encode one of $256$ distinct values.

Put together, this forms $65536$ unique combinations, for a total of $16$ bits.

You can assign the numbers from $0$ to $65535$ arbitrarily to these combinations, to form a bijection, and this bijection can be inverted.

For arbitrary assignments, you need to tabulate. For regular assignments (like bit interleaving f.i.), arithmetic transformations can do. There are million solutions.

You can even use target numbers larger than $16$ bits. In this case, there will be "holes" in the transformation, and values which are no the result of a packing cannot be unpacked.

But if your target variable cannot hold $16$ bits, what you are asking is impossible by the pigeonhole principle.

You could use base-256 for the two numbers, and have the two-"digit" base-256 number converted to base-10. To get the original numbers back, just convert the base-10 number back to base-256.

For example (with A=20 and B=2): $$(20,2)_{256} = 20*256 + 2 = 5124_{10}$$

The base-10 number can be easily converted back.

Function (input A, input B, output C):

• Set $C=256B+A$

Function (output A, output B, input C):

• Set $B=\lfloor{C/256}\rfloor$
• Set $A=C-256B$