views:

255

answers:

8

isPositive - return 1 if x > 0, return 0 otherwise

Example: isPositive(-1) = 0.

Legal ops: ! ~ & ^ | + << >>(arithmetic shift)

Max ops: 8

Note: No conditional statements are allowed. size of int is a word(4 bytes). It is a signed representation using two's compliment.

int isPositive(int x) {
  return ???;
}
A: 

if your working with a number system that uses the MSB as the signage bit, you can do:

int IsPositive(int x)
{
    return (((x >> 31) & 1) ^ 1) ^ !x;
}
Necrolis
Won't this return 1 for 0?
JoshD
seeing as there is no state for 0(ie: 0 is meant to be neither) its easier to have zero be treated as positive(imo), but it can be ammend to have it as negative(if needed)
Necrolis
Zero is not positive. The question is quite clear, return 1 if x > 0 return 0 otherwise. This does not do that.
JoshD
You need to add a `^!x` to handle the case where the parameter is 0: `return (((x >> 31) `. Note that even though the `!` operator isn't really a bitwise operator, it's in the question's permitted operations.
Michael Burr
@Micheal Burr: that would fix it :), corrected the answer
Necrolis
+4  A: 

you can do:

int isPositive(int x) {
   return !((x&(1<<31)) | !x);
}
codaddict
If I'm not mistaken, this fails for 0.
JoshD
@JoshD: That's what the requirement says.
codaddict
@codaddict, he says return 1 for x > 0. So doesn't that mean return 0 for x == 0? Zero is not positive.
JoshD
@codaddict - Should return 1 only if number is > 0.
Eternal Learner
@JoshD: Thanks for pointing. Added the check.
codaddict
+1 for catching the zero case.
JoshD
Konrad Rudolph
konforce
Eternal Learner
+1  A: 

Haven't done assembler for quite a while, but as far as I remember first digit in the word represents negative value e.g. 1000 is -8, hence if most significant bit is 1 the number is negative. So the answer is !(x>>31)

Vitalij
+1  A: 

Assuming a two’s complement representation (not always the case!), this can be achieved by testing whether the most significant bit is set (in which case the number is negative).

Notice that the following code uses illegal operations (+, * and -) but these are for clarity and platform independence only. If you know more about your particular platform, e.g. that int is a 32 bit number, the relevant constants can be replaced by their numeric value.

// Will be 1 iff x < 0.
int is_neg = (x & (INT_MAX + 1)) >> (CHAR_BIT * sizeof(int) - 1);
// Will be 1 iff x != 0.
int is_not_zero = !!x;
return !is_neg & is_not_zero;
Konrad Rudolph
+5  A: 
int isPositive(int x)
{
 return (!(x & 0x80000000) & !!x); 
}
Henrik
Almost, you need to shift this right 31 bits to get 1 rather than 0x80000000
JoshD
@JoshD: the result of ! is a bool which is mapped to 0 or 1.
Henrik
@Henrik: ooh, good call. This makes much of my code (and of the considerations for different platforms) completely redundant.
Konrad Rudolph
@Henrik: nuts. In his last question there was no !, I scalded my brain ... sorry for the invalid comment.
JoshD
@JoshD - yeah , the previous question had no !. If we could substitute other operators for it m then the solution might be easy .
Eternal Learner
+2  A: 
return !((x & 0x80000000) >> 31 | !x);
konforce
Necrolis
you can use (1 << 31) instead of 0x80000000
lalli
I included the ``.
konforce
+1 to Necrolis, it will fail on my pentium, but the question says int is 4 bytes...
lalli
Also - no need for the `>> 31` operation.
Michael Burr
Better to use `sizeof(int)*CHAR_BIT` to determine the number of bits in an int and then use the correct mask and shift values.
Paul R
@Michael, the constraint was only bitwise operators, so you need to put the 0x80000000 bit in the same position as the result of !x which is either 0 or 1. You need to shift one or the other....
RBerteig
If your computer uses 1's compliment then this will return true for -0 (do you want that)?
Martin York
+2  A: 

Let's play with the sign bit: sign(~n) : 1 if n >= 0

To get rid of the case when n is 0: sign(~n + 1) : 1 if n > 0 or n = MIN_INT

So, we want the case when both functions return 1:

return ((~n & (~n + 1)) >> 31) & 1;
ruslik
+1 for great explanation..
Eternal Learner
A: 

int isPositive(int x)

{

 return ( ! (x & ( 1 << 31 ) ) );

}

It will return 1 if given no is +ve and return 0 if given no is -ve

in this function we got sign bit if that is 1 it means no is -ve so we return 0 and if sign bit is 0 it means number is +ve so we return 1 .

Anand Kumar