Bit mask Created 2026-10-13 Updated 2026-10-20
A word selecting specified binary positions under a bitwise operation. To extract a stored bit at position , logically shift by and combine with the mask 1. Grouping thirty-two cells into one Java integer permits a compact representation of a Conway Game of Life board.
// Destination: computer-science.bigb
Count only the existing neighbours, excluding the center, then apply the Conway Game of Life transition:
java
static boolean nextCell(boolean[][] old, int i, int j) {
    int height = old.length;
    int width = old[0].length;
    int neighbours = 0;
    for (int di = -1; di <= 1; di++) {
        for (int dj = -1; dj <= 1; dj++) {
            if (di == 0 && dj == 0) continue;
            int r = i + di;
            int c = j + dj;
            if (r >= 0 && r < height && c >= 0 && c < width
                    && old[r][c]) {
                neighbours++;
            }
        }
    }
    return neighbours == 3 || (old[i][j] && neighbours == 2);
}
The short-circuit bounds test occurs before indexing a neighbour. Out-of-range cells therefore contribute zero, including at corners, and no opposite-edge wrapping occurs. The center is explicitly excluded. Birth requires three neighbours; survival requires two or three. The method accepts a nonempty rectangular board and a valid cell index and does not change the old Java array.
Use disjoint Java arrays for the old and new generations:
java
static void step(boolean[][] old, boolean[][] next) {
    for (int i = 0; i < old.length; i++) {
        for (int j = 0; j < old[i].length; j++) {
            next[i][j] = nextCell(old, i, j);
        }
    }
}
Every decision reads only old, so all cells undergo the simultaneous Conway Game of Life transition. The second board must have the same dimensions and share no row arrays with the first. Afterwards swap their references for the following step.
Writing each new cell immediately into the input board would expose a mixture of old and new states to later neighbour counts. The result would depend on the traversal order and would no longer be the specified simultaneous automaton. For example, put the three live cells at on an otherwise dead board. The simultaneous result is the vertical triple . During a naive row-major overwrite, becomes live first. The next cell now sees that new cell as well as the old cells , so it is incorrectly born with three counted neighbours. This explicitly exhibits the mixture of generations. A second generation array, or explicit preservation of overwritten old information, is essential.