C#和操作员说明 [英] C# & operator clarification

查看:85
本文介绍了C#和操作员说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到了一些关于&&&之间的区别的问题.和& C#中的运算符,但是我仍然很困惑如何使用它,以及在不同情况下会导致什么结果.例如,我只是在项目中浏览了以下代码

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

何时将得出0,何时将得出> 0?这个运算符背后的逻辑是什么?运算符'|'之间有什么区别?

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

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

解决方案

&"号对二进制表示形式的整数进行按位与运算. 管道按位或.

在此处查看这些按位运算的含义: http://en.wikipedia.org/wiki/Bitwise_operation

I saw a couple of questions here about the diference between && and & operators in C#, but I am still confused how it is used, and what outcome results in different situations. For example I just glimpsed the following code in a project

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

When it will result 0 and when >0? What is the logic behind this operator? What are the diferences between the operator '|'?

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

Can we use the &&, || operators and get the same results (possibly with different code)?

解决方案

The ampersand does bitwise AND on the integers in their binary representations. The pipe does bitwise OR.

See here what those bitwise operations mean: http://en.wikipedia.org/wiki/Bitwise_operation

这篇关于C#和操作员说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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