C语言 仅使用 C 中的按位运算符检查数字 x 是否为正数 (x>0)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3912375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:40:46  来源:igfitidea点击:

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

cbit-manipulation

提问by Eternal Learner

isPositive- return trueif x > 0, otherwise false

isPositive- 返回true如果x > 0,否则false

Example: isPositive(-1)

例子: isPositive(-1)

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

法律行动: !~&^|+<<>>

Max ops: 8

最大操作数: 8

Note: No conditional statements are allowed.

注意:不允许使用条件语句。

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

采纳答案by Matthew

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

回答by codaddict

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

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

x&(1<<31是检查数字是否为负数。

!xis to check if the number is zero.

!x是检查数字是否为零。

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

如果一个数字不是负数且不是零,那么它就是正数。

回答by Henrik

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

回答by Kira Gao

Why not use XOR (^)?

为什么不使用XOR (^)

Try this,

尝试这个,

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

It can deal with the 0 case well.

可以很好的处理0的情况。

回答by ruslik

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

让我们玩一下符号位:sign(~n): 1 if n >= 0

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

去掉nis 0的情况:sign(~n + 1): 1 if n > 0 or n = MIN_INT

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

所以,我们想要两个函数都返回 1 的情况:

return ((~n & (~n + 1)) >> 31) & 1;

回答by Konrad Rudolph

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 intis a 32 bit number, the relevant constants can be replaced by their numeric value.

请注意,以下代码使用了非法操作(+,*-),但这些只是为了清晰和平台独立。如果您对您的特定平台有更多的了解,例如,这int是一个 32 位数字,则相关常量可以替换为它们的数值。

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

回答by user9869932

You have another option here:

您还有另一个选择:

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

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

它只是一个 (0 & min(0,x))。

Test here

在这里测试

回答by Necrolis

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

如果您使用使用 MSB 作为标志位的数字系统,您可以执行以下操作:

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

回答by Vitalij

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)

已经有一段时间没有做汇编了,但据我所知,单词中的第一个数字表示负值,例如 1000 是 -8,因此如果最高有效位是 1,则该数字是负数。所以答案是!(x>>31)

回答by Anand Kumar

int isPositive(int x)

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

如果给定 no is +ve 它将返回 1 如果给定 no is -ve 则返回 0

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 .

在这个函数中,我们得到符号位,如果它是 1 则意味着 no is -ve 所以我们返回 0 并且如果符号位是 0 它意味着 number 是 +ve 所以我们返回 1 。