C 布尔逻辑
我一直在尝试用 C 语言编写一些程序,然后遇到了这个问题...
#include<stdio.h>
int main()
{
int j = 3, k;
k= !5 && j;
printf("%d", k);
return 0;
}
任何人都可以弄清楚这是什么问题吗?如果我编译该程序,结果将是 0。
当我在 C# 中尝试相同的代码时,
public void logic()
{
j = 5;
k = !4 && j;
Console.WriteLine("hence the value would be " + k);
}
这将是产生错误
( 错误 1 运算符“!”无法应用于“int”类型的操作数 C:\Documents and Settings\SANDEEP\My Documents\Visual Studio 2005\Projects\ConsoleApplication18\ConsoleApplication18\Program.cs 21 17 ConsoleApplication18 )< /p>
我想知道为什么我的 C 代码的输出不起作用,以及如何在 C# 中使用 !
运算符。 请帮忙。
I have been trying some programs in the C Language and come across to this...
#include<stdio.h>
int main()
{
int j = 3, k;
k= !5 && j;
printf("%d", k);
return 0;
}
Can anyone figure out what is the problem in this if I compile the program I will result to 0.
and when I tried the same code in C#
public void logic()
{
j = 5;
k = !4 && j;
Console.WriteLine("hence the value would be " + k);
}
this will generate the error
( Error 1 Operator '!' cannot be applied to operand of type 'int' C:\Documents and Settings\SANDEEP\My Documents\Visual Studio 2005\Projects\ConsoleApplication18\ConsoleApplication18\Program.cs 21 17 ConsoleApplication18 )
I want to know why the output of my C code is not working, and how can I use the !
operator in C#.
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 C 中,任何非零值(例如
5
)都被视为“true”,因此!5
计算结果为0
或“false” ”。因此,0 && 3
也是错误的。 C 中的逻辑运算符。在C#中,类型系统在这方面要强一些。
!
运算符仅适用于bool
类型的值,它完全独立于整数类型。请参阅! C# 中的运算符。In C, any non-zero value such as
5
is considered to be "true", therefore!5
evaluates to0
, or "false". Thus,0 && 3
is false as well. Logical operators in C.In C#, the type system is a little stronger in this respect. The
!
operator only works on values of typebool
, which is completely independent of the integer types. See ! Operator in C#.5
当解释为布尔值时为 true,因此!5
给出布尔结果 false,这会导致&&
给出 false,如下所示出色地。 False 解释为int
为0
。5
when interpreted as a boolean value is true, so!5
gives the boolean result false, which causes&&
to give false as well. False interpreted as anint
is0
.可读的C代码:
编译它、运行它以及如何计算表达式。
您可能无法应用“!”以这种方式转换为 C# 中的整数。我相信你可以通过在 int 上使用“~”来实现这一点。
您可以应用“!”为布尔值(我不知道在 C# 中如何调用它)。
The readable C code:
Compile it, run it, and how the expressions are evaluated.
You probably cannot apply "!" to ints in C# in this manner. I believe you can achieve that by using "~" on an int.
You can aply "!" to a Boolean (I don't know how it's called in C#).
C# 中此 C 代码的等效代码
为
注意,C# 不允许对整数使用布尔逻辑运算符。如果您想将整数转换为布尔值,则必须编写代码来执行此操作,就像我在这里所做的那样。
两个程序都正确输出零,因为 5 不等于 0,因此 !5 (C) 或 !(5!=0) (C#) 为 false。在 C 中, false 转换为零。在 C# 中,条件运算符的替代选项给出零。
这能回答你的问题吗?
The equivalent code to this C code
in C# would be
Notice that C# does not allow Boolean logic operators to be used on ints. If you want to convert ints to bools, you'll have to write code to do that, as I have here.
Both programs correctly output zero because 5 is not equal to 0, therefore !5 (C) or !(5!=0) (C#) is false. In C, the false converts to zero. In C#, the alternative of the conditional operator gives zero.
Does that answer your question?
这就是逻辑非运算符。您将它放在
bool
的右侧,它会产生相反的结果。在 C# 中,没有从整数到布尔值的隐式转换。顺便说一下,C# 中的“相同代码”有不同的数值!
That's the logical-not operator. You put it to the right of a
bool
and it produces the opposite. In C# there is no implicit conversion from integer tobool
.By the way, your "same code" in C# has different number values in it!
你正试图做一个逻辑“非”。您必须对 C# 中的 int 执行按位 NOT(~ 运算符,而不是!)。请参阅此处:
http://msdn.microsoft.com/ en-us/library/d2bd4x66(VS.71).aspx
You are trying to do a logical NOT. You have to do a bitwise NOT on an int in C# (the ~ operator, not !). See here:
http://msdn.microsoft.com/en-us/library/d2bd4x66(VS.71).aspx
在 C#(和 Java)中,您可以执行类似的操作
来获得与 C 程序相同的行为。将整数与 0 进行比较给出与 C 相同的“逻辑”。
In C# (and Java) you could do something like
to get the same behaviour as your C program. Comparing an integer to 0 gives the same "logics" than C.
如果您想要进行位算术而不是简单的布尔算术,则您的 C 代码使用了错误的运算符。这是示例链接,其中包含正确的内容运营商。
在您当前的 C 代码中,k=!5&&j,5 被认为是 true,因此 !5 是 0(假)。然后你将它与 j 进行 AND 运算,但是任何与任何布尔值的 false AND 运算都是 false,所以你得到 0。
你可能想要类似 (tilda5)&j 的东西
If you want to do bit arithmetic rather than simple boolean arithmetic, your C code is using the wrong operators. Here's a sample link with the correct operators.
In your current C code, k=!5&&j, 5 is considered true, so !5 is 0 (false). You then AND it with j, but any false ANDed with any boolean is false, so you get 0.
You probably want something like (tilda5)&j