A boolean array in computer programming is a sequence of values that can contain only the values of true or false. By definition, a boolean can only be true or false and cannot contain any other values in between. An array is a sequence of data types that occupy numerical positions in a linear memory space. While the actual implementation of a Boolean array is often left to the compiler or computer language libraries, it is most efficiently done using bits rather than full bytes or words. There are several uses for a Boolean array, including controlling property flags and aligning settings to physical hardware interfaces.
Woman doing handstand with a computer
The idea of a Boolean array originates from original methods used to store information on computers where there was very little memory available. The first implementation of a Boolean array took the form of an array of bits. This used larger data types, such as bytes or long integers, to store information, setting the data type bits to true or false. In this way, a single byte eight bits long can contain eight different values of true or false, saving space and allowing efficient bitwise operations.
As the size of computer memory has increased, the need to use bit arrays has decreased. While using bits provides the ability to shift bits and use logical operators that allow for incredibly fast processing, it also requires custom code to handle these types of operations. Using a standard array structure to store a sequence of bytes is a simpler solution, but it takes up much more memory during program execution. This can be seen when creating an array of 32 boolean values. With a bit array, the data will only take up four bytes of memory, but a boolean array can take up 32 to 128 bytes, depending on the system implementation.
Some computer programming languages actually implement an array of bits when a Boolean array type is used, although this is not common. A boolean array has the advantage of being very easy to read when viewing the source code. Comparisons and assignments are presented clearly, while with a bit array the logical operators “and”, “or” and “not” must be used, potentially creating confusing code.
Despite the ease of use, one feature that cannot be used with a boolean array is a bitmask. A bitmask is a single byte or larger data type that contains a sequence of true and false values related to various conditions. In a single operation, multiple bits can be checked for their true or false state all at once. With an integer-based array of boolean values, the same operation would need to be performed with a loop.