IsLess

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!

Comments

  • [code]
    int IsLess(int x,int y)
    {
    return x < y;
    }
    [/code]

    : 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!
    :
    :

  • I can't use < or > either. Just the operators listed and the = operator.


    : [code]
    : int IsLess(int x,int y)
    : {
    : return x < y;
    : }
    : [/code]
    :
    : : 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!
    : :
    : :
    :
    :

  • : I can't use < or > either. Just the operators listed and the = operator.
    :

    ##@#^^^^***(!!

  • TEll me about it!
    : : I can't use < or > either. Just the operators listed and the = operator.
    : :
    :
    : ##@#^^^^***(!!
    :
    :

  • : TEll me about it!
    : : : I can't use < or > either. Just the operators listed and the = operator.
    : : :
    : :
    : : ##@#^^^^***(!!
    : :
    : :
    :
    :
    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]
    : :
    To my mind your method is correct (~y+1 is (-y) so what your function does is x-y and then it checks whether the highest bit is set, in which case the number is negative and so x<y); however, in your example your function does not return the right answer because there is an overflow:
    2147483646-(-2) is greater than INT_MAX (0x7fffffff), and so the highest bit is set.
    There should be no problem with negative numbers provided that the sum x+(-y) is not greater than INT_MAX.

    I cannot help more now, but I will think about it.
    Take care, Steph.
  • : TEll me about it!
    : : : I can't use < or > either. Just the operators listed and the = operator.
    : : :
    : :
    : : ##@#^^^^***(!!
    : :
    : :
    :
    :
    The only thing I found is here. Add these tests to your function.
    [code]#include


    int isless(int x,int y)
    {
    if (x&~INT_MAX && !(y&~INT_MAX)) return 1; /*x<0 && y>=0*/
    if (y&~INT_MAX && !(x&~INT_MAX)) return 0; /*x>=0 && y<0*/
    return x+~y+1>>CHAR_BIT*sizeof (int)-1; /*same sign; notice parentheses are not mandatory because of precedence*/
    }[/code]

    I wrote this code so that it is not machine-dependent; it should work on every complementation machine.
    With these tests, the function should work for every integer.

    Take care, Steph.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories