Boolean data type explained

In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted true and false) which is intended to represent the two truth values of logic and Boolean algebra. It is named after George Boole, who first defined an algebraic system of logic in the mid-19th century. The Boolean data type is primarily associated with conditional statements, which allow different actions by changing control flow depending on whether a programmer-specified Boolean condition evaluates to true or false. It is a special case of a more general logical data type—logic does not always need to be Boolean (see probabilistic logic).

Generalities

In programming languages with a built-in Boolean data type, such as Pascal, C, Python or Java, the comparison operators such as > and ≠ are usually defined to return a Boolean value. Conditional and iterative commands may be defined to test Boolean-valued expressions.

Languages with no explicit Boolean data type, like C90 and Lisp, may still represent truth values by some other data type. Common Lisp uses an empty list for false, and any other value for true. The C programming language uses an integer type, where relational expressions like i > j and logical expressions connected by && and || are defined to have value 1 if true and 0 if false, whereas the test parts of if, while, for, etc., treat any non-zero value as true.[1] [2] Indeed, a Boolean variable may be regarded (and implemented) as a numerical variable with one binary digit (bit), or as a bit string of length one, which can store only two values. The implementation of Booleans in computers are most likely represented as a full word, rather than a bit; this is usually due to the ways computers transfer blocks of information.

Most programming languages, even those with no explicit Boolean type, have support for Boolean algebraic operations such as conjunction (AND, &, *), disjunction (OR, |, +), equivalence (EQV, =, ==), exclusive or/non-equivalence (XOR, NEQV, ^, !=, ¬), and negation (NOT, ~, !, ¬).

In some languages, like Ruby, Smalltalk, and Alice the true and false values belong to separate classes, e.g., True and False, respectively, so there is no one Boolean type.

In SQL, which uses a three-valued logic for explicit comparisons because of its special treatment of Nulls, the Boolean data type (introduced in) is also defined to include more than two truth values, so that SQL Booleans can store all logical values resulting from the evaluation of predicates in SQL. A column of Boolean type can be restricted to just TRUE and FALSE though.

Language-specific implementations

ALGOL and the built-in BOOLEAN type

One of the earliest programming languages to provide an explicit BOOLEAN data type is ALGOL 60 (1960) with values true and false and logical operators denoted by symbols '

\wedge

' (and), '

\vee

' (or), '

\supset

' (implies), '

\equiv

' (equivalence), and '

\neg

' (not). Due to input device and character set limits on many computers of the time, however, most compilers used alternative representations for many of the operators, such as AND or 'AND'.

This approach with BOOLEAN as a built-in (either primitive or otherwise predefined) data type was adopted by many later programming languages, such as Simula 67 (1967), ALGOL 68 (1970),[3] Pascal (1970), Ada (1980), Java (1995), and C# (2000), among others.

C, C++, D, Objective-C, AWK

Initial implementations of the language C (1972) provided no Boolean type, and to this day Boolean values are commonly represented by integers (ints) in C programs. The comparison operators (>, ==, etc.) are defined to return a signed integer (int) result, either 0 (for false) or 1 (for true). Logical operators (&&, ||, !, etc.) and condition-testing statements (if, while) assume that zero (and hence a NULL pointer or a null string terminator '\0' also) is false and all other values are true.

After enumerated types (enums) were added to the American National Standards Institute version of C, ANSI C (1989), many C programmers got used to defining their own Boolean types as such, for readability reasons. However, enumerated types are equivalent to integers according to the language standards; so the effective identity between Booleans and integers is still valid for C programs.

Standard C (since C99) provides a Boolean type, called _Bool. Since C23, the Boolean is now a core data type called bool, with values true and false (previously these were provided by macros from the header stdbool.h, which is now obsolete). The language guarantees that any two true values will compare equal (which was impossible to achieve before the introduction of the type). Boolean values still behave as integers, can be stored in integer variables, and used anywhere integers would be valid, including in indexing, arithmetic, parsing, and formatting. This approach (Boolean values are just integers) has been retained in all later versions of C. Note, that this does not mean that any integer value can be stored in a Boolean variable.

C++ has had the Boolean data type bool since C++98, but with automatic conversions from scalar and pointer values that are very similar to those of C. This approach was adopted also by many later languages, especially by some scripting languages such as AWK.

The D programming language has a proper Boolean data type bool. The bool type is a byte-sized type that can only hold the value true or false.The only operators that can accept operands of type bool are:

Notes and References

  1. Book: Brian W . Kernighan . Brian Kernighan . Dennis M . Ritchie . Dennis Ritchie . 41 . . 1st . . 1978 . . 0-13-110163-3 .
  2. Book: 86–93 . PJ . Plauger . P. J. Plauger . Jim . Brodie . ANSI and ISO Standard C Programmer's reference . . 1989 . 1992 . 1-55615-359-7 .
  3. Web site: Report on the Algorithmic Language ALGOL 68, Section 10.2.2. . August 1968 . 30 April 2007 . live . https://web.archive.org/web/20080406061108/http://www.fh-jena.de/~kleine/history/languages/Algol68-Report.pdf . 6 April 2008 .