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

后端 未结 12 653
夕颜
夕颜 2020-12-14 10:24

isPositive - return true if x > 0, otherwise false

Example: isPositive(-1)

Legal ops:

12条回答
  •  忘掉有多难
    2020-12-14 10:29

    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).

提交回复
热议问题