gpt4 book ai didi

C# 和运算符说明

转载 作者:可可西里 更新时间:2023-11-01 07:52:36 26 4
gpt4 key购买 nike

我在这里看到了几个关于 C# 中 && 和 & 运算符之间区别的问题,但我仍然对它的使用方式以及在不同情况下产生的结果感到困惑。例如我刚刚在一个项目中瞥见如下代码

bMyBoolean = Convert.ToBoolean(nMyInt & 1);
bMyBoolean = Convert.ToBoolean(nMyInt & 2);

什么时候结果为 0,什么时候 >0?这个运算符背后的逻辑是什么?运算符“|”之间有什么区别?

bMyBoolean = Convert.ToBoolean(nMyInt | 1);
bMyBoolean = Convert.ToBoolean(nMyInt | 2);

我们可以使用&&,||吗?运算符并得到相同的结果(可能使用不同的代码)?

最佳答案

&& 是条件语句,用于 if 语句和 while

if(x>1 && y<3)

这意味着x应该大于1,y应该小于3,同时满足这两个条件

if(x>1 || y<3)

满足其中之一

但是,& 和 |分别是按位与和或。例如:

 1 | 0  => 1
1 & 0 => 0
1 & 1 => 1

如果这适用于直接整数,则将计算并应用它们对应的二进制值

2&1
=> 10 // the binary value of 2
&
01 // the binary value of 1
--
00 // the result is zero

关于C# 和运算符说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12600062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com