Eternal Learner
Eternal Learner

Reputation: 3870

Check if a number x is positive (x>0) by ONLY using bitwise operators in C

isPositive - return true if x > 0, otherwise false

Example: isPositive(-1)

Legal ops: ! ~ & ^ | + << >>

Max ops: 8

Note: No conditional statements are allowed.

inline bool isPositive(int32_t x) {
  return ???;
}

Upvotes: 17

Views: 38981

Answers (12)

user9869932
user9869932

Reputation: 7377

You have another option here:

int is_positive = (0&x^((0^x)&-(0<x)));

It is just a (0 & min(0,x)).

Test here

Upvotes: 0

Kira Gao
Kira Gao

Reputation: 31

Why not use XOR (^)?

Try this,

{
    return ((x>>31)&1)^(!!x); 
}

It can deal with the 0 case well.

Upvotes: 3

Muthuraman
Muthuraman

Reputation: 163

int x,len;
x = 0x0fffffff;
len = (sizeof(x) * 8) - 2;

if ((x>>len))
 printf("Negative\n");
else
 printf("Positive\n");

X will be either int or char(Integral type).

Upvotes: -1

Kapil
Kapil

Reputation: 1

return !((x >> 31) & 1); This is to check MSB.

Upvotes: -2

codaddict
codaddict

Reputation: 455312

int isPositive(int x) {
   return !((x&(1<<31)) | !x);
}

x&(1<<31 is to check if the number is negative.

!x is to check if the number is zero.

A number is positive if it's not negative and not zero.

Upvotes: 19

Anand Kumar
Anand Kumar

Reputation: 499

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 .

Upvotes: -1

ruslik
ruslik

Reputation: 14890

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;

Upvotes: 3

Necrolis
Necrolis

Reputation: 26181

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;
}

Upvotes: 0

Matthew
Matthew

Reputation: 48304

return !((x & 0x80000000) >> 31 | !x);

Upvotes: 12

Konrad Rudolph
Konrad Rudolph

Reputation: 545923

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;

Upvotes: 1

Henrik
Henrik

Reputation: 23324

int isPositive(int x)
{
 return (!(x & 0x80000000) & !!x); 
}

Upvotes: 10

Vitalij
Vitalij

Reputation: 4625

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)

Upvotes: 0

Related Questions