Is there a way to create a number from two others in a way that they can be found afterwards?

42 Views Asked by At

I have to find a way to send two variables between 1 and 8 between two robots, but the IR function only allows one. Is there a way to make a number out of these two that can be easily transformed back into the original variables ?

For example, if var1=5 and var2=7, I want to be able to make a var3 that is easily transformed back into var1=5 and var2=7 for the robot.

Just for information, the robot I use is a ThymioII, it's documentation can be found there : https://www.thymio.org/en:asebalanguage

1

There are 1 best solutions below

1
On BEST ANSWER

To expand on Juanito's suggestion, if you encode like so:

c = 10 * a + b

Then you can decode like so:

a = c / 10
b = c % 10

This assumes that the / operator in Aseba is integer division rounding down.

If you prefer to work in binary, you can take advantage of the fact that Aseba integers are $16$ bits wide to encode with shifts:

c = (a << 8) + b

And decode:

a = c >> 8
b = c & 0xff

The first scheme will be more convenient if you will need to print out or otherwise debug the values of $c$ in decimal notation, so that $a=5$ and $b=7$ gets encoded as 57. The second scheme will be more convenient if you'll be viewing values in hexadecimal, so that $a=5$ and $b=7$ gets encoded as 0x0507.