Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|cite|improve this question
The square (-3,-3) actually has a value of 48, not 49. That tripped me up for a fair bit of time... – El'endia Starman Feb 6 '12 at 8:08
If I understand correctly, you are considering a pairing operation that combines two signed integers into one unsigned integer (the correspondence $(x,y)\mapsto n$) in the manner suggested by the illustration, and you are looking for explicit formulas for the pairing operation and the projections back $n\mapsto x$ and $n\mapsto y$. The reference to bitwise operations seems spurious; one could use them if convenient but not necessarily. The "diagonal" values $(0,8,24,48,80,\ldots)=(4(n^2+n))_{n\in\mathbf N}$ suggest they are of limited utility. – Marc van Leeuwen Feb 6 '12 at 8:28
@El'endia Starman -- Woops! Yep, you're right, my bad. Fixed! – Qix Feb 6 '12 at 9:54
@Marc van Leeuwen - Interesting about the diagonals. I devised a way to collapse the values using bitwise operators (two nibbles with a min/max of -7/7 each, collapsed into a single byte). However, the mathematical approach is far more interesting. – Qix Feb 6 '12 at 9:55
up vote 1 down vote accepted

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

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.