Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does chess& = board[i] mean?

Tags:

c

char chess = _board[0];
    for (char i = 1; i < 9; ++i)
    {
        chess &= _board[i];  //?
    }

board is a 1-d char array. what does chess& = board[i] mean? Thank you.

like image 489
Josh Morrison Avatar asked Nov 28 '25 09:11

Josh Morrison


2 Answers

The following statement:

x &= y

... is equivalent to:

x = x & y

... and, in the above, the ampersand (&) is the "bitwise and". The result of "x & y" is a value that has a 1 in all the bits where both x and y have corresponding 1 bits, and everywhere else the bits are 0.

like image 53
Michael Aaron Safyan Avatar answered Nov 29 '25 21:11

Michael Aaron Safyan


&= is the compound bitwise AND and assignment operator. What it does is that it takes the bits in chess (which is 8 bits as a char) and ANDs them with the corresponding bits of _board[i]. Then it assigns the results back to chess.

In effect, together with the loop this causes each bit of chess to be set if all of the values in _board has the corresponding bit set.

Further: since _board is 8 items times 8 bits, I 'll assume that each item represents a row and the bits of the item represent the columns a-h of that row. The presence of an 1 in a bit position represents something (we don't know what).

So the end result would be that chess contains an 1 in each of its 8 bits if and only if the something exists all rows on the corresponding column a-h of the chessboard.

like image 44
Jon Avatar answered Nov 29 '25 23:11

Jon