KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I need the most efficient way (in cpu cycles) to determine if two numbers have the same/different sign. But the catch is if either number is zero I need to be able to distinguish it from numbers with same/different signs (ie. zero is treated as a "third" sign ). The following code is similar to what I need, but the return values can be anything as long as there are only three distinct return values. int foo(int x, int y) { if (x * y > 0) return 1; if (x * y < 0) return -1; return 0; } For my specific problem , the values are in the range [-6, 6] and X is guaranteed to not be 0. I found a solution to find if two numbers have the same sign, and altered it to get the following solution. return y? (((x^y) >= 0)? 1 : -1) : 0; There should be some bitops/comparisons that give faster results than using multiplication, branching, comparisons.
Tags (comma-separated)
Save Edits
Cancel