I have 2 decimal values: a and b. How do I use bit operator to check if two value is same sign?
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.
}
Do you mean if both are positive or both are negative?
bool bothSameSign = (d1 >= 0 && d2 >= 0) || (d1 < 0 && d2 < 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With