Hi All,
Do the below without any conditional or comparison operator.
if (Number <= 0)
{
Print '0';
}
else
{
print Number;
}
thanks..
Hi All,
Do the below without any conditional or comparison operator.
if (Number <= 0)
{
Print '0';
}
else
{
print Number;
}
thanks..
My original simple solution:
1. print( (abs(Number)+Number) / 2 )
That solution would work in most cases, unless Number is very large (more than half the maximum e.g. Number >= MAX_INT/2) in which case the addition may cause overflow.
The following solution solves the overflow problem:
2. print( (abs(Number)/2) + (Number/2) )
However, there may be a case in which Number is and must remain integer, and the division operator (/) is integer division, so that 7/2=3. In this case solution 2 won't work because if Number=7 it will print 6 (for this case solution 1 will work just fine).
So if we need to deal with both large numbers AND integer arithmetic, the following monstrosity comes to the rescue, adding compensation for the 1 that may be lost in the division by 2 in case of odd integer:
3. print(
( (abs(Number)/2)+(Number/2) ) +
((
(Number-(2*(Number/2))) +
(abs(Number)-(2*(abs(Number)/2)))
) / 2)
)
Assuming C or C++:
switch ((unsigned long)Number & ~(unsigned long)LONG_MAX) {
case 0:
printf("%d\n", Number);
break;
default:
printf("0\n", Number);
break;
}
If you consider switch to be a conditional operator, then try this:
unsigned long flag = (unsigned long)Number & ~(unsigned long)LONG_MAX;
flag /= (unsigned long)LONG_MAX + 1;
flag = 1 - flag;
printf("%d\n", Number * flag);
I haven't seen a solution yet that is valid for the complete domain.
An other solution is to call a function that raises an exception if the input value is 0 or below 0. Then catch the exception and print 0.
Or you can use a function Sign, that returns -1 if the input is <0, 0 if it's 0 and 1 otherwise.
print ((sign(x)+1) * sign(x) / 2) * x.
sign can be -1, 0 or 1, so ((sign(x)+1) * sign(x) / 2) can have the following values:
-1 -> ((-1+1)*-1)/2 = 0
0 -> ((0+1)*0)/2 = 0
1 -> ((1+1) * 1)/2 = 1
Another method is to create a lookup table that maps all non negative numbers to themself and the rest to 0.
But, in my opinion, the original function is much clearer. So why violate the KISS principle.
Let's say that number is represented by an 8-bit two's complement integer.
Positive numbers including 0 all have the MSB set to 0.
Negative numbers all have the MSB set to 1.
So we take the complement of the MSB, extend it to the full 8 bits, and bitwise AND it with the original number, e.g.
Positive:
00110101 -> MSB is 0
11111111 -> complement of MSB extended
00110101 -> bitwise AND of above
Negative:
10110101 -> MSB is 1
00000000 -> complement of MSB extended
00000000 -> bitwise AND of above
No comparisons needed - I'm kind of assuming that bitwise AND isn't strictly a comparison.
Also, sorry for the lack of code, but you get the idea.
Similar to the accepted answer. Although acceptable where absolute value is implemented using comparisons, but also more prone to overflow:
print( (sqrt(Number * Number) + x) / 2);