Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how to check same sign of 2 decimal values using bit?

Tags:

c#

I have 2 decimal values: a and b. How do I use bit operator to check if two value is same sign?

like image 837
TPL Avatar asked Jun 24 '13 09:06

TPL


2 Answers

You can use Math.Sign(). When you use Math.Sign(x), if x is negative it returns -1 else if its positive, the function returns 1 or when its 0 it returns 0. So :

if(Math.Sign(a) == Math.Sign(b))
{
    // Code when sign matched.
}
else
{
    // Code when sign not matched.
}
like image 186
Writwick Avatar answered Nov 14 '22 23:11

Writwick


Do you mean if both are positive or both are negative?

bool bothSameSign = (d1 >= 0 && d2 >= 0) || (d1 < 0 && d2 < 0);
like image 45
Tim Schmelter Avatar answered Nov 14 '22 22:11

Tim Schmelter