什么是C#一个符号的第二个含义是什么? [英] What is the second meaning of a single ampersand in C#?

查看:176
本文介绍了什么是C#一个符号的第二个含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了单号(&安培;)在C#中的意思是检查第二的条件,即使是第一次声明中的



但下面似乎是&放一个不同的意义 干脆,可以谁能解释如何 I和1 在下面的示例

 列表< INT>分数=新的List< INT> {1,2,3,4,5,6,7,8}; 
变种evenScores = scores.Where(ⅰ= I标记%2 == 0);
变种evenScores2 = scores.Where(ⅰ= GT;(I和1)== 0);


解决方案

一个单独的放大器&;是按位与运算符,就像鸽子说。我看问题的第二部分:为什么?它的工作原理



想想二进制:

  000 = 0 
001 = 1
010 = 2
011 = 3
100 = 4
101 = 5
110处= 6
111 = 7

请注意所有偶数数字0结束;因此,如果对1返回最后位按位检查零(意思是不匹配),它是一个偶数;


I have used the single ampersand (&) in C# to mean "check the second conditional statement even if the first is false".

But the following seems to be a different meaning of & altogether, can anyone explain how i & 1 works in the following example?

List<int> scores = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
var evenScores = scores.Where(i => i % 2 == 0);
var evenScores2 = scores.Where(i => (i & 1) == 0);

解决方案

A single & is "Bitwise AND operator", just like dove said. I'm looking at second part of question: "why it works?"

Think in binary:

 000 = 0
 001 = 1
 010 = 2
 011 = 3
 100 = 4
 101 = 5
 110 = 6
 111 = 7
 and so on

Note all even numbers ends with 0; so if last bit bitwise check against 1 returns zero (meaning "doesn't match"), its a even number;

这篇关于什么是C#一个符号的第二个含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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