I have a 1D array. Each element in the array is arranged to emulate a 3D index:

```
for (int x = 0; x < depth; ++x)
{
for (int y = 0; y < height; ++y)
{
for (int z = 0; z < width; ++z)
{
int index = x * width * height + y * width + z;
```

For each element I need to check all neighbouring elements to see if they exist. This means that a 3D array would produce a maximum of 26 possible neighbours per element.

I first have to check to see if the neighbouring element is in range using a set of booleans:

```
// Array indices
int leftIndex = x - 1;
int rightIndex = x + 1;
int bottomIndex = y - 1;
int topIndex = y + 1;
int farIndex = z - 1;
int nearIndex = z + 1;
bool left = leftIndex >= 0;
bool right = rightIndex < width;
bool bottom = bottomIndex >= 0;
bool top = topIndex < height;
bool far = farIndex >= 0;
bool near = nearIndex < depth;
```

With these boolean values I can determine if each one of the 26 neighbours exist.

How can I set up the boolean values so that I don't need to keep checking them for each neighbour?

In other words I can determine each neighbour as follows:

```
if (left && top && far)
if (top && far)
if (right && top && far)
if (left && top)
if (top)
// etc
```

but this will mean checking a boolean condition more than once.

Is there a way to use a |= operator with these boolean values and simplify them so they add up to a number? This can can be used with a switch case statement that knows which neighbours can be created?

Or can someone think of an alternative method to prevent repeating boolean checks for each neighbour?