1st, it returns true if both sides of "&" are true.

& returns the bitwise and of two variables not the logical and (ie &&).

All the cardinal directions(NORTH, SOUTH, EAST, and WEST) directions all have only one leading bit whereas the other directions have some combination. If you and a value with itself minus one you will only get a non-zero value if there is more than one bit set.

1st, it returns true if both sides of "&" are true.
2nd, if D = 2 (South), both will be true.
3rd, if D = 5 (Northeast), both are still true.

HOW DOES THAT WORK!?

EDIT: After more look, 1 (North) and 0 (Center) will return false.

D=2 returns false (2&1==0), so there's a minor flaw in your analysis, but here's how it looks in binary:

20: 10100 <u>& 19: 10011</u> 16 10000

By subtracting 1 from a number, the rightmost "on" bit is turned off, and all others to the right turn on. AND those two together and the result is to turn off the rightmost bit.

So the test if(D&D-1) returns a nonzero value only if D has 2 or more bits set. If D is 0 or a power of 2, the test is false. Since all of BYOND's directions are bit flags, they all have a single on bit; the diagonal directions combine those, for two on bits.