Hi,
I am having problem with the following code.This is on a 32 bit system. The problem statement is:
/* isLess - if x < y then return 1, else return 0
* Example: isLess(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
*/
I can't use any loops or - * / !! || < >
My code so far is:
int isLess(int x, int y) {
return (( x + (~y + 1)) >> 31 ) & 1 ;
}
It works for all numbers except for negative numbers:
Test isLess(2147483646[0x7ffffffe],-2[0xfffffffe]) failed.
Gives 1[0x1]. Should be 0[0x0]
I am new to programming and am very green when it comes to the negative numbers!
Any help is appreciated!