Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

This function take 2 integer and if both int are positive return 0, if the first is positive and the second is negative return 1, if both are negative return 2 and if the first is negative and the second is positive return 3.

share|improve this question

closed as off topic by Asaf Karagila, The Chaz 2.0, Beni Bogosel, Kannappan Sampath, Rasmus Apr 20 '12 at 21:18

Questions on Mathematics Stack Exchange are expected to relate to math within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

11  
What about this function: $$f(x,y)=\begin{cases} 0& x>0, y>0\\ 1& x>0, y<0\\ 2& x<0,y<0\\ 3& x<0,y>0\\ ??& x=0\text{ or }y=0\end{cases}$$ –  Asaf Karagila Apr 20 '12 at 19:54
1  
@Pioz, What if both are zero? –  Julius Apr 20 '12 at 19:57
2  
Pioz, what you have described is a function from $\mathbb{Z} \times \mathbb{Z} \setminus (0,0) \to \mathbb{Z}$ –  The Chaz 2.0 Apr 20 '12 at 19:57
3  
See what happened here? Two users came to MSE especially for a question and an answer that would have been more than reasonable on SO but verges off-topic on MSE. –  Asaf Karagila Apr 20 '12 at 19:59
1  
This is hardly a math question. –  copper.hat Apr 20 '12 at 20:02
show 2 more comments

3 Answers 3

up vote 3 down vote accepted

In C your may write (with ^ the 'xor') :

(y<0)^(3*(x<0))

share|improve this answer
add comment

Consider a one-argument function $g(x)$ that is equal $1$ if $x$ is negative, and $0$ if $x$ is positive.

Then a possible answer is $$f(x,y)=2\cdot g(x)+g(y)$$

Now pick you favorite $g(x)$, for example, $$g(x)=\frac{1-sign(x)}{2}$$

share|improve this answer
add comment

Maybe this will help:

#include <math.h>

int func(int a, int b)
{
    int sa = (a > 0) - (a < 0);
    int sb = (b > 0) - (b < 0);
    return (-sa+1) + (-sb+1)/2;
}

It works this way:

+---+---+----+----+------+
| a | b | sa | sb | func |
+---+---+----+----+------+
| + | + | +1 | +1 |    0 |
| + | - | +1 | -1 |    1 |
| - | + | -1 | +1 |    2 |
| - | - | -1 | -1 |    3 |
+---+---+----+----+------+

But it wrong when a or b is equal 0.

Here you can read a lot about bit hack: http://graphics.stanford.edu/~seander/bithacks.html

share|improve this answer
add comment

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