Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Stonicus

Opposite signs

This topic is 5230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!