Bitwise Operations: Collapsing two values into one.

472 Views Asked by At

Alright, so I'm having a hard time thinking of a way to do this. Perhaps someone here can help!

Here is the problem: I need to figure out a way to use bitwise operations (or any sort of equation) to turn a single number into an x and y that represents an offset spiraling 0,0 (see image).

Related image:

Spiral Model

So $f(n) = x$ and $g(n) = y$, for example, would yield something like $f(25) = -2$ and $g(4)= 1$.

I need a way to store those two values within a byte, either through bitwise operations (much like how RGB values are stored and retrieved within an integer) or through some other mathematical equation.

If the numbers need to be repositioned or something, that is fine too.

Any help to point me in the right direction?

1

There are 1 best solutions below

0
On BEST ANSWER

KK so each x,y intercept increase/decrease has a difference of 8.
A x+ and a y+ or a x- and a y- has a difference of 2 Where X would be a greater value.
A x+ and a y- or a x- and a y+ has a difference of 2 Where Y would be a greater value.

Realized Just now...
On the Y+ difference Scale
1 - 0 = 1
10 - 1 = 9
27 - 10 = 17
52 - 27 = 25
And so on.. (Difference of 8 Like I said before)

Now on the X+ difference Scale
3 - 0 = 3
14 - 3 = 11
33 - 14 = 19
60 - 33 = 27
And So on...(Dif of 8)

Now... These are the x+ and the y+ intercept values..
If I want to combine these values like so..

X...Y...Diff ..Actually means
3 , 1 = 2.......1x and 1y
11, 9 = 12.....2x and 2y
19, 17 = 30...3x and 3y
27, 25 = 56..4x and 4y