Opposite signs

Started by
2 comments, last by Stonicus 20 years ago
What''s the most efficient way to determine if two integers are of different signs?

bool DiffSigns(int i, int j)
{
  if(((i > 0) && (j < 0)) || ((i < 0) && (j > 0)))
    return true;
  return false;
}
 
This will work, it''s the first function that comes to mind (it ignores the zero case as the purpose I need it for will handle it ahead of time) but it seems there is always a more efficient way than the first method you can think of. So any ideas?
Advertisement
bool diffSigns(const int a, const int b){  return bool(int(a < 0) ^ int(b < 0));}   
Evil, but it works.

[edited by - Oluseyi on March 23, 2004 5:18:26 PM]
bool DiffSigns(int i, int j){  return (bool)((i^j)&0x8000000)} 


now THAT''s evil

(should work assuming the compiler takes all non 0 values as true)
http://mitglied.lycos.de/lousyphreak/
Not as evil...
bool diffSigns(const int a, const int b)
{
return (a < 0) != (b < 0);
}


[edited by - dalleboy on March 23, 2004 5:32:01 PM]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

This topic is closed to new replies.

Advertisement