# How would it be possible to encoding a row and a column with a single number? But the row or column or both are negative

I know that you can represent a row and column in a single number by multiplying the row by a number and adding the column, for example 5 * 8 + 3 = 43 And 43 is: row: 8/43 = 5.375 = 5 column: 43 mod 8 = 3

And that number 43 would have the information from row to column, you would just have to decode it (so to speak)

But what happens if the row or the column or both are negative? How could I do that, because I don't see how I could store it in a way that I can decode it and retrieve the row and column with their negative values.

For example: (-5, 3) (5, -3) (-5, -3)

Most commonly we count rows and columns starting at $$0$$ or $$1$$ so there is no problem. If you do have negative rows and columns there are two solutions. If there is a minimum row and column number you can just add enough to make the numbers start at $$0$$. For example, if the rows and columns go down to $$-10$$ you add $$10$$ to the row and column numbers and then encode it the way you are thinking. When you decode you follow your process and subtract $$10$$ at the end. The constant $$5$$ needs to be at least the number of columns. Alternately, you can double the positive row numbers and double, negate, and subtract $$1$$ from the negative row numbers. In this case the processed row number is even if the original is zero or positive and odd if the original is negative. You can unpack this in the other direction without trouble.