或、与运算符

发布于 2024-09-01 11:05:45 字数 50 浏览 4 评论 0原文

新手问题。 如何计算公式 A f B 的值,其中 f - 二元函数 OR 或 AND?

Newbie question.
How to calculate the value of the formula A f B, where f - the binary function OR or AND?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

怂人 2024-09-08 11:05:45

条件运算符 && 之间是有区别的。和||以及布尔运算符 &和|。主要是优先级的差异(首先评估哪些运算符)以及 && 。和||正在‘逃避’。这意味着这是一个序列,例如...

cond1 && cond2 && cond3

如果 cond1 为 false,则 cond2 或 cond3 都不会被求值,因为代码正确地假设无论它们的值是什么,表达式都不能为 true。同样...

cond1 || cond2 || cond3

如果 cond1 为 true,则 cond2 或 cond3 都不会被求值,因为无论它们的值是什么,表达式都必须为 true。

按位对应, &和 |没有逃避。

希望有帮助。

There is a distinction between the conditional operators && and || and the boolean operators & and |. Mainly it is a difference of precendence (which operators get evaluated first) and also the && and || are 'escaping'. This means that is a sequence such as...

cond1 && cond2 && cond3

If cond1 is false, neither cond2 or cond3 are evaluated as the code rightly assumes that no matter what their value, the expression cannot be true. Likewise...

cond1 || cond2 || cond3

If cond1 is true, neither cond2 or cond3 are evaluated as the expression must be true no matter what their value is.

The bitwise counterparts, & and | are not escaping.

Hope that helps.

梦醒时光 2024-09-08 11:05:45

逻辑或为||,逻辑与为&&
如果您需要否定 NOT,请在表达式前加上 ! 前缀。

示例:

X = (A && B) || C || !D;

当 A 和 B 为真或 C 为真或 D 不为真(即假)时,X 将为真。

如果您想要按位与/或/非,您可以使用 &|~。但如果您正在处理布尔/真值,则您不想使用它们。例如,由于按位运算的工作方式,它们不提供短路评估。

Logical OR is ||, logical AND is &&.
If you need the negation NOT, prefix your expression with !.

Example:

X = (A && B) || C || !D;

Then X will be true when either A and B are true or if C is true or if D is not true (i.e. false).

If you wanted bit-wise AND/OR/NOT, you would use &, | and ~. But if you are dealing with boolean/truth values, you do not want to use those. They do not provide short-circuit evaluation for example due to the way a bitwise operation works.

¢好甜 2024-09-08 11:05:45
if(A == "haha" && B == "hihi") {
//hahahihi?
}

if(A == "haha" || B != "hihi") {
//hahahihi!?
}
if(A == "haha" && B == "hihi") {
//hahahihi?
}

if(A == "haha" || B != "hihi") {
//hahahihi!?
}
疏忽 2024-09-08 11:05:45

我不确定这是否能回答您的问题,但例如:

if (A || B)
{
    Console.WriteLine("Or");
}

if (A && B)
{
    Console.WriteLine("And");
}

I'm not sure if this answers your question, but for example:

if (A || B)
{
    Console.WriteLine("Or");
}

if (A && B)
{
    Console.WriteLine("And");
}
雪花飘飘的天空 2024-09-08 11:05:45

使用“&&”对于 AND 并使用“||”对于 OR,例如:

bool A;
bool B;

bool resultOfAnd = A && B; // Returns the result of an AND
bool resultOfOr = A || B; // Returns the result of an OR

Use '&&' for AND and use '||' for OR, for example:

bool A;
bool B;

bool resultOfAnd = A && B; // Returns the result of an AND
bool resultOfOr = A || B; // Returns the result of an OR
小清晰的声音 2024-09-08 11:05:45

如果您感兴趣的是按位运算,请在此处查看简短教程:http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor- and-amp-amp-not.aspx .bitwise 操作执行与上面示例相同的操作,它们只是使用二进制表示(该操作适用于值的每个单独位)

如果您想要逻辑运算答案是已经给了。

If what interests you is bitwise operations look here for a brief tutorial : http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx .bitwise operation perform the same operations like the ones exemplified above they just work with binary representation (the operation applies to each individual bit of the value)

If you want logical operation answers are already given.

月野兔 2024-09-08 11:05:45

&&仅当两个操作数都为 true 时,该操作才会返回 true,这意味着

bool and(bool b1, bool b2)]
{
 if(b1==true)
 {
   if(b2==true)
    return true;
 }
 return false;
}

||如果一个或两个操作数为 true,则它的操作返回 true,这意味着

bool or(bool b1,bool b2)
{
 if(b1==true)
 return true;
 if(b2==true)
 return true;
 return false;
}

如果您

y=45&&34//45 binary 101101, 35 binary 100010

在结果中写入,则有

y=32// in binary 100000

因此,我上面写的内容用于每对位

&& it's operation return true only if both operand it's true which implies

bool and(bool b1, bool b2)]
{
 if(b1==true)
 {
   if(b2==true)
    return true;
 }
 return false;
}

|| it's operation return true if one or both operand it's true which implies

bool or(bool b1,bool b2)
{
 if(b1==true)
 return true;
 if(b2==true)
 return true;
 return false;
}

if You write

y=45&&34//45 binary 101101, 35 binary 100010

in result you have

y=32// in binary 100000

Therefore, the which I wrote above is used with respect to every pair of bits

紙鸢 2024-09-08 11:05:45

上面有很多答案,我将尝试一种不同的方式:

如果您正在寻找按位运算,则仅使用其中一个标记,例如:

3 & 1 //==1 - 和
4 | 1 //==5 - 或

many answers above, i will try a different way:

if you are looking for bitwise operations use only one of the marks like:

3 & 1 //==1 - and
4 | 1 //==5 - or

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文