# Boolean data type: Wikis

Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.

# Encyclopedia

In computer science, the Boolean or logical data type is a primitive data type having one of two values: true or false, intended to represent the truth values of logic and Boolean algebra.

In programming languages that have a built-in Boolean data type, such as Pascal and Java, the comparison operators such as '>' and '≠' are usually defined to return a Boolean value. Also, conditional and iterative commands may be defined to test Boolean-valued expressions.

Languages without an explicit Boolean data type, like C and Lisp, may still represent truth values it by some other data type. Lisp uses an empty list for false, and any other value for true. C uses an integer type, with false represented as the zero value, and true as any non-zero value (such as 1 or -1). Indeed, a Boolean variable may be regarded (and be implemented) as a numerical variable with a single binary digit (bit), which can store only two values.

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

In some languages, the Boolean data type is defined to include more than two truth values. For instance the ISO SQL 1999 standard defined a Boolean value as being either true, false, or unknown (SQL null). Although this convention defies the law of excluded middle, it is often useful in programming.

In the lambda calculus model of computing, Boolean values are represented as Church booleans.

## History

One of the earliest languages to provide an explicit Boolean data type was 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 limitations of the time, however, most compilers used alternative representations for the latter, such as AND or 'AND'. This approach ("Boolean is a separate built-in primitive data type") was adopted by many later languages, such as ALGOL 68 (1970) [1], Java, and C#.

The first version of FORTRAN (1957) and its successor FORTRAN II (1958) did not have logical values or operations; even the conditional IF statement took an arithmetic expression and branched to one of three locations according to its sign. FORTRAN IV (1962), however, followed the Algol 60 example by providing a Boolean data type (LOGICAL), truth literals (.TRUE. and .FALSE.), Boolean-valued numeric comparison operators (.EQ., .GT., etc.), and logical operators (.NOT., .AND., .OR.). In FORMAT statements, a specific control character ('L') was provided for the parsing or formatting of logical values.[2]

The Lisp programming language (1958) never had a built-in Boolean data type. Instead, conditional constructs like cond assume that the logical value "false" is represented by the empty list (), which is defined to be the same as the special atom nil or NIL; whereas any other s-expression is interpreted as "true". For convenience, most modern dialects of Lisp predefine the atom t to have value t, so that one can use t as a mnemonic notation for "true". This approach ("any value can be used as a Boolean value") was retained in most Lisp dialects (Common Lisp, Scheme, Emacs Lisp), and similar models were adopted by many scripting languages; although which values are interpreted as "false" and which are "true" vary from language to language. In Scheme, for example, the "false" value is an atom distinct from nil, so the latter is interpreted as "true". In Python, a numeric value of zero (integer or fractional), the null value (None), and empty containers (i.e. strings, lists, sets, etc.) are considered boolean false; all other values are considered boolean true by default. In Ruby programming language, on the other hand, only the null object and a special false object are "false", everything else (including the integer 0) is "true".

Some languages use a variant of this approach: they do have a distinct built-in Boolean data type (or at least built-in distinctive values for "false" and "true"), but any value is automatically converted to a Boolean value if used in a context that requires the latter. This is the approach used by PHP, JavaScript, and by Python since version 2.3 (which treats Boolean values as 0 or 1 in arithmetic contexts).

The initial standards for the C programming language (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 zero (for false) or 1 (for true). The same convention is assumed by the logical operators ('&&', '||', '!', etc.) and condition-testing statements ('if', 'while'). Thus logical values 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") was retained in all later versions of C. Some of its dialects, like C99 and Objective C, provide standard definitions of a Boolean type as a synonym of int and macros for "false" and "true" as 0 and 1, respectively. Visual Basic uses a similar approach. C++ has a separate Boolean data type ('bool'), 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 ones such as Awk and Perl. One problem with this approach is that the tests if(t==TRUE){...} and if(t) are not equivalent.

The Pascal programming language (1978) introduced the concept of programmer-defined enumerated types. A built-in Boolean data type was then provided as a predefined enumerated type with values FALSE and TRUE. By definition, all comparisons, logical operations, and conditional statements applied to and/or yielded Boolean values. Otherwise, the Boolean type had all the facilities which were available for enumerated types in general — such as ordering and use as indices. On the other hand, the conversion between Booleans and integers (or any other types) still required explicit tests or function calls, as in Algol 60. This approach ("Boolean is an enumerated type") was adopted by most later languages which had enumerated types, such as Modula, Ada and Haskell.

After enumerated types ('enum's) were added to the ANSI version of C (1989), many C programmers got used 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 most C programs and libraries.

### Special approaches

In recent versions of Python, user-defined objects may specify their truth value by providing a __bool__ method.[3]

JavaScript lacks a Boolean data type proper, but has a Boolean object class which can be used as a wrapper for storing and handling its two Boolean values. However, paradoxically, such a Boolean object will be automatically interpreted as "true" in Boolean contexts, even if its stored value is "false".

As of the 1999 standard, SQL specified a Boolean data type with four possible values: true, false, unknown or null. However, vendors could choose to equate the last two values.[4]. Because of this inconsistency most SQL implementations (with the notable exception of Postgresql[5]) use other data types (like bit, byte, and character) to simulate Boolean values.

## References

1. ^ "Report on the Algorithmic Language ALGOL 68, Section 10.2.2." (PDF). August 1968. Retrieved April 2007.
2. ^ Digital Equipment Corporation, DECSystem10 FORTRN IV Programmers Reference Manual. Reprinted in Mathematical Languages Handbook. [www.bitsavers.org/.../Tymcom-X_Reference_Series_Fortran_IV_Jan73.pdf Online version] accessed 2009-06-21.
3. ^ http://docs.python.org/3.1/reference/datamodel.html#object.__bool__
4. ^ ISO/IEC. ISO/IEC 9075-1:1999 SQL Standard (pdf format). Section 4.4.3.3. 1999.
5. ^ http://www.postgresql.org/docs/7.4/static/datatype-boolean.html