7
77scrapper77
hi,
I need to write a function that will check whether x is nonzero. Return 0
if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
<< >> and I am not allowed to use an loops.
My program so far looks like:
int isNonZero(int x) {
x = (1 << x) & x;
x = ~x;
x = x & 1;
return x;
}
All cases work except for the below.
Test isNonZero(-2147483648[0x80000000]) failed.
Gives 0[0x0]. Should be 1[0x1]
If tried everything I could think of and I am all out of ideas. Any one
have any idea how I could fix my code to deal with this negative number?
I also need to write one that will check whether x is nonzero using only ~
& ^ | + << >>
My code so far is:
int isLess(int x, int y) {
int a =0;
a = x ^ y;
a = y >> a;
a = a + 1;
a = !a;
return !a;
}
Which only works for about half the cases (on small non-negative numbers)
Any input would be appreciated.
Thanks,
I need to write a function that will check whether x is nonzero. Return 0
if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
<< >> and I am not allowed to use an loops.
My program so far looks like:
int isNonZero(int x) {
x = (1 << x) & x;
x = ~x;
x = x & 1;
return x;
}
All cases work except for the below.
Test isNonZero(-2147483648[0x80000000]) failed.
Gives 0[0x0]. Should be 1[0x1]
If tried everything I could think of and I am all out of ideas. Any one
have any idea how I could fix my code to deal with this negative number?
I also need to write one that will check whether x is nonzero using only ~
& ^ | + << >>
My code so far is:
int isLess(int x, int y) {
int a =0;
a = x ^ y;
a = y >> a;
a = a + 1;
a = !a;
return !a;
}
Which only works for about half the cases (on small non-negative numbers)
Any input would be appreciated.
Thanks,