I'm trying to figure out why in the following method the last value in the array '8' would fulfill the where clause of "(8 & 7) == 0."
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 + " ");
}
}
It's included on a recruitment skills test, and for the life of me I can't figure out why that value is selected. Either way I won't be using it on my tests, but I'm curious since I've never run across this before.
So a single &
is a bitwise operator. It's looking at those numbers binary representation.
7 = 0111 and 8 = 1000
When you combine them you get 0.
That is why your method prints out a 9.