C语言 如何在C中使用逆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6508585/
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
how to use inverse in C
提问by samprat
[how to use ~ operator ]
[如何使用~运算符]
I have a structure say Alpha. I know the value of element inside Alpha(say a) which can be 0or 1- I want the other element of same structure to take inverse value of Alpha.a. For example:
我有一个结构说Alpha。我知道内部元素的值Alpha(比如a)可以是0或1- 我希望相同结构的另一个元素取 Alpha.a 的反值。例如:
if Alpha.a = 1
then Alpha.b = 0
and vice versa
反之亦然
I have tried:
我试过了:
Alpha.b = ~ (Alpha.a)
But unfortunately it doesnt work - when Alpha.ais 1, Alpha.bgets set to 254
但不幸的是它不工作-当Alpha.a是1,Alpha.b被设置为254
Any ideas?
有任何想法吗?
Thanks and regards,
感谢致敬,
SamPrat
桑普拉特
回答by David Given
In C, trueis represented by 1, and falseby 0. However, in a comparison, any non-false value is treated is true.
在 C 中,true用 1 表示,false用 0表示。但是,在比较中,任何非假值都被处理为真。
The !operator does booleaninversion, so !0 is 1 and !1 is 0.
该!运算符执行布尔反转,因此 !0 为 1,!1 为 0。
The ~operator, however, does bitwiseinversion, where every bit in the value is replaced with its inverse. So ~0 is 0xffffffff (-1). ~1 is 0xfffffffe (-2). (And both -1 and -2 are considered as true, which is probably what's confusing you.)
~但是,该运算符执行按位反转,其中值中的每一位都被替换为其倒数。所以 ~0 是 0xffffffff (-1)。~1 是 0xfffffffe (-2)。(并且 -1 和 -2 都被认为是正确的,这可能会让您感到困惑。)
What you want is !, instead of ~.
你想要的是!, 而不是~.
回答by Prince John Wesley
Use XOR operator:
使用异或运算符:
Alpha.b = Alpha.a ^ 1;
回答by Lindydancer
The ~operator negates each individual bit. For example, assume that Alpha.ais an unsigned char. Then ~1would read, in binary as, ~00000001, and the result would be 11111110 (again, in binary), which is the same as 254 in decimal and 0xFE in hex.
该~运营商否定每个位。例如,假设它Alpha.a是一个unsigned char. 然后~1以二进制形式读取 ~00000001,结果将是 11111110(再次以二进制形式),这与十进制的 254 和十六进制的 0xFE 相同。
As others have suggested, use !Alpha.aor Alpha.a ^ 1.
正如其他人所建议的那样,使用!Alpha.a或Alpha.a ^ 1。
回答by Bruce Ricker
A nice cross-platform cross language solution to this common problem is:
这个常见问题的一个很好的跨平台跨语言解决方案是:
Alpha.b = 1 - Alpha.a;
回答by Thorben
What about a postoperational bitmask?
后操作位掩码怎么样?
using unsigned chars looking for bit0:
使用无符号字符查找 bit0:
b = 0x01u & ( ~a );
or even
甚至
b = a ^ 0x01u;
or for "Boolean-Thinkers" ( be aware TRUE and FALSE may differ from 0/1 ! if you want it "real boolean" you should use TRUE and FALSE if defined.)
或者对于“Boolean-Thinkers”(注意 TRUE 和 FALSE 可能与 0/1 不同!如果你想要它“真正的布尔值”,你应该使用 TRUE 和 FALSE 如果定义。)
b = (1 != a)?(0u):(1u);
Cheers
干杯
回答by Chris Snowden
You can't use ~as this will turn 00000000into 11111111rather than 00000001as I think you're expecting.
你不能使用,~因为这会00000000变成11111111而不是00000001我认为你期望的。
If you have bools you can use:
如果你有布尔值,你可以使用:
Alpha.b = !(Alpha.a)
but if not you may have to use if / else logic:
但如果不是,您可能必须使用 if / else 逻辑:
if (Alpha.a == 0)
{
Alpha.b = 1;
}
else
{
Alpha.b = 0;
}
回答by Seth Robertson
You want to use another operator. Specifically !
您想使用其他运算符。具体来说 !
Alpha.b = !Alpha.a
Since the values are zero or one, it is much simplier.
由于这些值为零或一,因此要简单得多。

