C#如何评估数组循环布尔等式?
How does C# Evaluate an array loop Boolean equilancy?
我想弄清楚为什么在下面的方法中,数组“8”中的最后一个值会满足“(8 & 7) == 0”的 where 子句。
public class Test {
public static void Main() {
int[] Arr = {-3, -1, 0, 1, 3, 8};
var s = from x in Arr where (x & (x-1)) == 0 select x+1;
foreach (int x in s)
Console.Write(x + " ");
}
}
它包含在招聘技能测试中,但我终究无法弄清楚为什么选择该值。无论哪种方式,我都不会在我的测试中使用它,但我很好奇,因为我以前从未 运行 穿过这个。
所以单个 &
是位运算符。它正在查看那些数字的二进制表示。
7 = 0111 和 8 = 1000
将它们组合起来会得到 0。
这就是您的方法打印出 9 的原因。
我想弄清楚为什么在下面的方法中,数组“8”中的最后一个值会满足“(8 & 7) == 0”的 where 子句。
public class Test {
public static void Main() {
int[] Arr = {-3, -1, 0, 1, 3, 8};
var s = from x in Arr where (x & (x-1)) == 0 select x+1;
foreach (int x in s)
Console.Write(x + " ");
}
}
它包含在招聘技能测试中,但我终究无法弄清楚为什么选择该值。无论哪种方式,我都不会在我的测试中使用它,但我很好奇,因为我以前从未 运行 穿过这个。
所以单个 &
是位运算符。它正在查看那些数字的二进制表示。
7 = 0111 和 8 = 1000
将它们组合起来会得到 0。
这就是您的方法打印出 9 的原因。