Boolean data type
| This is an old revision of this page, as edited by Cybercobra (talk | contribs) at 11:58, 10 December 2009 (→Python: update for 3.0). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision. |
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 '
' (and), '
' (or)',
' (implies), '
' (equivalence), and '
' (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] and Java.
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. In Python, for example, the truth value "false" can be represented by a numeric value of zero (integer or fractional), the null vale ('None'), or an empty string, list, or set.
The initial standards for the C programming language (1972) provided no Boolean type; and, to this day, Boolean values are commonly represented by integers 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, and was adopted by many later languages, especially by some scripting ones such as Awk and Perl.
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.
Boolean types and operations in modern languages
|
|
It has been suggested that this article be merged into the respective programming language articles. (Discuss) Proposed since June 2009. |
Ada
Ada (defined in 1979, implemented in 1983) defines Boolean in the package Standard as an enumerated type with values False and True where False < True.
type Boolean is (False, True); p : Boolean := True; ... if p then ... end if;
The relational operators (=, /=, <, <=, >, >=) apply to all enumerated types, including Boolean. Boolean operators and, or, xor, and not are defined on Boolean and any declared subtype. The Boolean operators also apply to arrays of Boolean values.
C
To this day, Boolean values are commonly represented by integers in C programs. The comparison operators (' > ', '==', etc.) are defined to return a signed integer (int) result, either zero (for false) or nonzero (for true). The Boolean operators (&&, ||) and conditional statements (if, while) in C operate on integer values, with the same interpretation. For example, the following C code
int t = (x > y); if (t) { printf("True!\n");} else { printf("False!\n"); }
is equivalent to
int t; if (x > y) { t = -1; } else { t = 0; } if (t != 0) { printf("True!\n"); } else { printf("False!\n"); }
However, since the C language standards allow the result of a comparison to be any non-zero value, the statement if(t==1){...} is not equivalent to if(t){...} or to if(t!=0){...}.
Since C standards mandate that 0 be interpreted as the null pointer when used in a pointer context or cast to a pointer, one can test whether a pointer is non-null by the statement if(p){...} instead of if(p!=NULL){...} — although some code styles discourage this shorthand. One can also write if(x){...} when x is a floating-point value, to mean if(x!=0){...}.
For convenience, many programmers and C header files use C's typedef facility to define a Boolean type (which may be named Boolean, boolean, bool, etc.). The Boolean type may be just an alias for a numeric type like int or char. In that case, programmers often define also macros for the true and false values. For example,
typedef int bool; #define FALSE 0 #define TRUE (-1) ... bool f = FALSE; ... if (f) { ... }
The defined values of the TRUE and FALSE macros must be adequate for the chosen Boolean type. Note that, on the now common two's complement computer architectures, the signed value -1 is converted to a non-zero value (~0, the bit-wise complement of zero) when cast to an unsigned type, or assigned to an unsigned variable.
Another common choice is to define the Boolean type as an enumerated type (enum) allows naming elements in the language of choice. For example, the following code uses the English names FALSE and TRUE as possible values of the type boolean. In this case, care must be taken so that the false value is represented internally as a zero integer:
typedef enum { FALSE, TRUE } boolean; ... boolean b = FALSE; ... if (b) { ... }
Again, since a true result may be any non-zero value, the tests if(t==TRUE){...} and if(t), which are equivalent in other languages, are not equivalent in C.
C99
The C99 version of C provides a built-in _Bool data type. It is large enough to store the values 0 and 1. When any scalar value is converted to _Bool, the result is 0 if the value is 0, otherwise 1.
If the <stdbool.h> is #included, the macros bool, true and false can be used to refer to _Bool, 1 and 0, respectively:
#include <stdbool.h> int main() { bool b = false; b = true; }
These macros bool, true and false are unrelated to the C++ boolean type, and their use in programs that mix C and C++ may lead to incompatibilities.[citation needed]
C++
During its standardization process, the C++ programming language introduced the bool, true and false keywords, adding a native data type to support Boolean data. Its size is implementation-defined.[3] bool was introduced in 1993[4].
Values of type bool are either true or false.[5] Implicit conversions exist between bool and other integral types, floating point types, pointer types, and pointer-to-member types.[6] In addition, user-defined types can be converted to bool via a user-defined conversion operator. In general, a zero or null-pointer value is converted to false, any other value is converted to true.
#include <string> int main() { using std::string; // implicit conversions to bool bool a = 'a'; // char -> bool [true] bool b = 0.0; // double -> bool [false] bool c = -1; // int -> bool [true] bool d = 0; // int -> bool [false] bool e = &a; // bool* -> bool [true] bool f = &string::clear; // void (string::*)() -> bool [true] // implicit conversions from bool int i = false; // bool -> int [0] double j = true; // bool -> double [1.0] char* k = false; // bool -> char* [(char*)(0)] }
The 1998 C++ Standard Library defines a specialization of the vector template for bool. The description of the class indicates that the implementation should pack the elements so that every bool only uses one bit of memory.[7] This is widely considered a mistake[8][9]. vector<bool> does not meet the requirements for a C++ Standard Library container. For instance, a container<T>::reference must be a true lvalue of type T. This is not the case with vector<bool>::reference, which is a proxy class convertible to bool.[10] Similarly, the vector<bool>::iterator does not yield a bool& when dereferenced. There is a general consensus among the C++ Standard Committee and the Library Working Group that vector<bool> should be deprecated and subsequently removed from the standard library, while the functionality will be reintroduced under a different name[11]. This change is unlikely to take place until after C++0x.
C#
In C#, Boolean variables are identified through the reserved word bool, which is an alias for the predefined struct type System.Boolean. It occupies four bytes. No standard conversions exist between bool and other types.
Code to output a Boolean could be represented like this:
bool myBool = (i == 5); System.Console.WriteLine(myBool ? "I = 5" : "I != 5");
Haskell
In Haskell, a Bool type is defined in the standard prelude, defining it as a simple algebraic data type:
data Bool = False | True
The operations are defined as ordinary functions and operators:
(&&), (||) :: Bool -> Bool -> Bool True && x = x False && _ = False True || _ = True False || x = x
Java
In the Java programming language, Boolean variables are represented by the primitive type boolean. The Java Virtual Machine (JVM) abstracts away from the actual representation in memory, so JVM writers can represent Boolean values in whatever manner is convenient (for example, one byte, or one word).
The Java Language Specification does not permit any explicit or implicit casts to or from boolean. Thus, it requires the compiler to reject this code:
int i = 1; if (i) System.out.println("i is not zero."); else System.out.println("i is zero.");
because the integer variable i cannot be cast to a Boolean type, and the if statement requires a boolean condition.[12]
In Java, boolean values (like other primitive types) can be appended to Strings. This feature provides a default visual representation of a Boolean value (true is displayed as "true" and false as "false").[12]
Another way to use Boolean values is to set a variable type as a Boolean type. This can be done in the following:
boolean i; //the variable i is boolean i = true; //sets the value to true if (i){ System.out.println("i is true"); }else { System.out.println("i is not true"); }
boolean used with a switch statement:
boolean i = true; //we can also set the value of the variable in the same line that it is being declared switch(i){ case true: System.out.println("i is true"); break; case false: System.out.println("i is not true"); break; }
JavaScript
JavaScript has two keywords true and false, both of which are written in lowercase. It is a weakly typed language and does not have an explicit Boolean data type for its variables. However many values will evaluate to false when used in a logical context, including zero, null, zero length strings, and unknown properties of objects. All other variable values, including empty arrays and empty objects, will evaluate to true. The language does offer a Boolean object which can be used as a wrapper for handling Boolean values. The Boolean object will always evaluate to true even if it has a value of false.
var objBool = new Boolean(false); if ( false || 0 || "" || null || window.not_a_property ) { alert("never this"); } else if ( true && [] && {} && objBool ) { alert("Hello Wikipedia"); // will bring up this message }
Lua
Lua defines a boolean type which can be either true or false as of version 5.0.[13] In addition to the value false, the special value nil (of type nil) also evaluates to false in logical expressions. In versions prior to 5.0, only the value nil evaluated to false (and conditional expressions which were false resulted in the value nil being returned).[14] The usefulness of the values true and false are limited in certain situations because the logical operators and and or return one of their arguments as the result instead of the value true or false. This construct allows logical operations in Lua to act similar to C's Ternary Operator, ?:
local a, b = 1, 2; local function isTrue(condition, message) print((message or "<nothing> (which becomes 'nil')") .. " is " .. (condition and "TRUE" or "FALSE")) end -- All of the below display "TRUE" isTrue(true, "true") isTrue(0, "0") isTrue(1, "1") isTrue("Foo", "Foo") isTrue(a == a, "a == a") -- All of the below display "FALSE" isTrue(false, "false") isTrue(nil, "nil") isTrue() isTrue(a == b, "a == b") isTrue(c, "c") isTrue(a == c, "a == c")
ML
Like Ocaml, ML has a bool type that has true and false values. For example:
- fun isittrue x = if x then "YES" else "NO" ; > val isittrue = fn : bool -> string - isittrue true; > val it = "YES" : string - isittrue false; > val it = "NO" : string - isittrue (8=8); > val it = "YES" : string - isittrue (7=5); > val it = "NO" : string
Objective-C
Objective-C provides a type BOOL, and macros YES and NO. Since Objective-C is a superset of C, C language semantics for booleans also apply.
Ocaml
Ocaml has a bool type that has true and false values.
# 1 = 1 ;; - : bool = true
Like other enumerated types, a value of this type uses a word of memory.
Pascal
Boolean is a basic data type provided by Pascal. Its definition and uses:
(* declaration in system or standard*) Type Boolean = (False,True); (* usage *) var value: Boolean; ... value := True; value := False; if value then begin ... end;
Note that values outside the enum are not defined. Some compilers like Delphi have as extension special Boolean type that map onto C numeric types for interfacing purposes. (Delphi: bytebool,wordbool,longbool)
Perl
In the Perl programming language, there is no distinction between numbers, strings, and other non-aggregate data types. (They are all called "scalar".) Aggregate types without any elements, empty strings, numbers which equal a value of 0, the strings "" and "0", and undefined variables evaluate to "false" when used in a Boolean context. All other values (including strings such as 0.0 and 0E0 which are "zero but true") evaluate to "true".
Elements of aggregates may also be tested against "existence" or "non-existence"[1], and all variables may be evaluated as either "defined" or "undefined".[2] (An element of a hash or array that has been assigned the value undef exists but is undefined.) In Perl this distinction is important when evaluating scalars in a Boolean manner to prevent "false falses" where one of the above values should be considered "true".
There are no built-in true or false constants in Perl 5, however the values do exist internally in Perl6.
1 is traditionally used for true, and constructs such as ... while 1 are special-cased to avoid advisory warnings. Internally, recent versions of Perl 5 have a variety of predefined "yes" and "no" values, so the recommended way to provide a false value has recently shifted from undef to !1 .
PHP
PHP has a Boolean data type [15] with two values: true and false (case doesn't matter).
$var = true; $var = false; print $var ? "T" : "F"; print $var == true ? "T" : "F"; print $var === true ? "T" : "F"; print is_bool($var) ? "T" : "F"; print gettype($var);
Several values evaluate to a logical false [16] with the loose comparison operator ==. There are generally empty instances of a type, or are considered equivalent to the number 0. These values are:
- false
- integer or float 0
- string "0"
- NULL
- empty array
- empty string
PHP programmers wishing to distinguish a Boolean variable set to false from other types of variable must use the strict comparison operator ===.
Python
The Python programming language allows all objects to be tested for their truth value. The following values are considered false:
- Numeric zero, None, False.
- Empty containers such as empty strings, lists, tuples, dicts and sets.
- User defined object instances have control over their Boolean value through special methods
__bool__[17] and__len__.
In all other cases, objects are considered true.
An explicit bool type was added to the language in version 2.3, consisting of the values True and False, which can be used in arithmetic expressions as 1 and 0.[18]
Boolean operators and Boolean built-in types always return one of the Boolean values True and False except for the operators "or" and "and" which return one of their operands (from left to right, the first operand that determines the Boolean value of the expression).[19]
>>> class spam: pass # spam is assigned a class object. ... >>> eggs = "eggs" # eggs is assigned a string object. >>> spam == eggs # (Note double equals sign for equality testing). False >>> spam != eggs # != and == always return bool values. True >>> spam and eggs # and returns an operand. 'eggs' >>> spam or eggs # or also returns an operand. <class __main__.spam at 0x01292660> >>>
Ruby
The Ruby programming language does not have a Boolean data type as part of the language. Like many other interpreted languages, all variables are dynamically typed. Instead, ruby defines the explicit values of false and nil, and everything else is considered true, including 0, [ ], and the empty string "". The values true, false, and nil can be assigned to variables, returned from functions or methods, and compared in Boolean expressions.
a = 0 if a puts "true" else puts "false" end
will print "true", which might come as a surprise to a new user of the language.
Since Ruby is a pure object-oriented programming language, even the "explicitly" defined values of true, false and nil are objects that each have their own class:
p false.class p true.class p nil.class
Would output "FalseClass", "TrueClass" and "NilClass" respectively.
Scheme
Scheme has two special symbols #t and #f which represent the logical values of true and false respectively. However, any non-#f value is interpreted as true. Note that unlike Lisp, nil or '(), the empty list, is separate from #f in Scheme, and therefore is considered true.
SQL
SQL supports three-valued logic (3VL), and comparison predicates in SQL can return any of three possible results: true, false, or unknown. The Boolean data type was introduced in the ISO SQL:1999 standard, which specified that in addition to the three possible SQL Boolean values, instances of the data type could be set to null[20]. For DBMSs that implement the ISO SQL:1999 standard, the following code creates a table which holds instances of the Boolean data type.
CREATE TABLE test1 ( a INT, b BOOLEAN ); INSERT INTO test1 VALUES (1, TRUE); INSERT INTO test1 VALUES (2, FALSE); INSERT INTO test1 VALUES (3, NULL); -- The SQL:1999 standard says that vendors can use null in place of the -- SQL Boolean value unknown. It is left to the vendor to decide if -- null should be used to completely replace unknown. The standard also -- says that null should be treated as equivalent to unknown, which is an -- inconsistency. The following line may not work on all SQL:1999-compliant -- systems. INSERT INTO test1 VALUES (4, UNKNOWN); SELECT * FROM test1;
The SQL Boolean data type did not gain widespread adoption, owing to inconsistencies in the standard and lack of support from vendors. Most SQL DBMSs use other data types like bit, byte, and char to simulate the behavior of Boolean data types. Postgresql supports the standard SQL Boolean data type.[21]
Visual Basic
In Visual Basic Boolean values from comparisons can be stored in variables with the Boolean data type, which is stored as a 16-bit signed integer, but should only have the values True(-1) and False(0). For example:
Dim isSmall As Boolean isSmall = intMyNumber < 10 ' Expression evaluates to True or False If isSmall Then MsgBox("The number is small") End If Dim hellFreezesOver As Boolean ' Boolean variables are initialized as False hellFreezesOver = False ' Or you can use an assignment statement Do Call CheckAndProcessUserInput() Loop Until hellFreezesOver
Because Boolean are stored in 16 bits, coercion or passing of values from other languages (e.g. via COM) may take other values. This is dangerous since the Visual Basic runtime uses an exclusive OR operation for negation; other values turn "True" into "True" violating the law of excluded middle. This can lead to subtle bugs that are extremely hard to detect.
Sub Voo(ByRef v As Variant) v = 1 End Sub Sub Bar(ByRef b As Boolean) b = 1 End Sub Dim b1 As Boolean, b2 As Boolean b1 = True b2 = True Debug.Print (b1 = b2) 'True Call Voo(b2) Debug.Print (b1 = b2) 'False Call Bar(b2) Debug.Print (b1 = b2) 'True
XPath and XQuery
XML Path Language (XPath 2.0) and XML Query Language (XQuery 1.0) both rely on XML Schema for Boolean data type support. The XML Schema xs:boolean data type supports both true and false Boolean values. XPath and XQuery define a set of rules for calculating the effective Boolean value of expressions.
XPath 1.0 and languages based on it, like XML Stylesheet Language (XSL), also support Boolean data types and implicit calculation of effective Boolean values from non-Boolean expressions.
See also
- true and false shell scripting commands
- Shannon's expansion
References
- ^ "Report on the Algorithmic Language ALGOL 68, Section 10.2.2." (PDF). August 1968. Retrieved April 2007.
- ^ 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.
- ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §5.3.3 Sizeof [expr.sizeof] para. 1
- ^ "Evolving a language in and for the real world: C++ 1991-2006" (PDF). 2007. Retrieved March 2008.
- ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §3.9.1 Fundamental types [basic.fundamental] para. 6
- ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §4.7 Integral conversions [conv.integral] para. 4
- ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §23.2.5 Class vector<bool> [lib.vector.bool] para. 1
- ^ "vector<bool>: More Problems, Better Solutions" (PDF). August 1999. Retrieved May 2007.
- ^ "A Specification to deprecate vector<bool>". March 2007. Retrieved May 2007.
- ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §23.2.5 Class vector<bool> [lib.vector.bool] para. 2
- ^ "96. Vector<bool> is not a container". Retrieved January 2009.
- ^ a b Java Language Specification, 3rd edition - online at http://java.sun.com/docs/books/jls/
- ^ "Lua Manual 5.1".
- ^ "The Evolution of an Extension Language: A History of Lua".
- ^ PHP: Booleans - Manual
- ^ PHP: PHP type comparison tables - Manual
- ^ http://docs.python.org/3.1/reference/datamodel.html#object.__bool__
- ^ "http://python.org/doc/2.3.5/whatsnew/section-bool.html".
- ^ "Boolean operations".
- ^ ISO/IEC. ISO/IEC 9075-1:1999 SQL Standard (pdf format). Section 4.4.3.3. 1999.
- ^ http://www.postgresql.org/docs/7.4/static/datatype-boolean.html
|
||||||||||||||||||||||||||||||