使用与放大器;与&功放;&安培; [英] Usage & versus &&

查看:161
本文介绍了使用与放大器;与&功放;&安培;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到这个疑问,并希望这里的一些人能够帮助我在这个

I came across this doubt and hope some one here would be able to help me out on this

bool Isvalid = isValid & CheckSomething()

bool Isvalid = isValid && CheckSomething()



第二种情况可能是短路

second case could be scenario for short circuiting

所以,我们不能总是只使用&安培; 代替&放大器;&安培;

So can't we always use just & instead &&.

推荐答案

&安培;是逐位与,这意味着它的工作原理对位水平。 &功放;&安培;是逻辑与意为它工作在布尔(真/假)的水平。逻辑与使用短路(如果第一部分是假的没有使用检查的第二部分),以防止运行多余的代码,而逐位,需要和每一个位,所以它会得到的值。

& is a bit-wise "AND", meaning that it works on the bit level. && is a logical "AND" meaning it works at boolean (true/false) level. Logical AND uses short-circuiting (if the first part is false there's no use checking the second part) to prevent running excess code while bit-wise AND needs to and every bit so it will get the value.

您应该使用逻辑与(安培;&安培;),因为这是你想要的东西(安培;可以potientally没有做正确的事),但你可能需要单独运行的方法,如果你想评价它的副作用:

You should use logical AND (&&) because that's what you want (& could potientally not do the right thing), but you may need to run the method separately if you want to evaluate its side effects:

var check = CheckSomething();
bool IsValid = isValid && check;

这篇关于使用与放大器;与&功放;&安培;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆