- Boolean datatype
In
computer science , the Boolean datatype, sometimes called the "logical datatype", is aprimitive datatype having one of two values: true and false. Many systems represent true as"non-zero" (often 1, or -1) and false as "zero". It is the special case of a binary numeric datatype of only one digit, orbit , and can also be represented in any otherradix by restricting the range of allowed values for certain operations.In some languages the Boolean datatype is defined to represent more than two truth values. For instance the ISO SQL:1999 standard defined a Boolean data type for SQL which could hold three possible values: true, false, unknown (SQL null is treated as equivalent to the unknown truth value, but only for the Boolean data type).
This datatype is used in Boolean and other operations such as and (
AND
,&
,*
), or (OR
,|
,+
), exclusive or/not equivalent (xor
,NEQV
,^
), equal (EQV
,=
,=
) and not (NOT
,~
,!
) which correspond to some of the operations of Boolean algebra andarithmetic .Ada
Ada defines
Boolean
in the package Standard as an enumerated type with valuesFalse
andTrue
whereFalse
<True
.The relational operators (
=
,/=
,<
,<=
,>
,>=
) apply to all enumerated types, includingBoolean
. Boolean operatorsand
,or
,xor
, andnot
are defined onBoolean
and any declared subtype. The Boolean operators also apply to arrays ofBoolean
values.Algol
Algol 60 had aBoolean
datatype and associated operations, defined in the Algol 60 report. This was abbreviated tobool
inALGOL 68 . [] .The 1998 C++ Standard Library defines a specialization of the
vector
class. To optimize space, the elements are packed so that every bool only uses one bit of memory. This is widely considered a mistake.vector
does not meet the requirements for a STL container. For instance, acontainer
::reference must be a true
lvalue of typeT
. This is not the case withvector
. Similarly, thevector
does not yield a::iterator bool&
when dereferenced. There is a general consensus among the C++ Standard Committee and the Library Working Group thatvector
should be deprecated or entirely removed from the next version of the standard. []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 offalse
andnil
, and everything else is consideredtrue
, including 0,[ ]
, and the empty string""
. The valuestrue
,false
, andnil
can be assigned to variables, returned from functions or methods, and compared in Boolean expressions.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 oftrue
,false
andnil
are objects that each have their own class:Would output "FalseClass", "TrueClass" and "NilClass" respectively.
cheme
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.QL
SQL supports three-valued logic (3VL), and comparison predicates in SQL can return any of three possible results: true, false, or unknown. The Boolean datatype was introduced in the ISO SQL:1999 standard, which specified that in addition to the three possible SQL Boolean values, instances of the datatype could be set to nullISO/IEC. [http://www.ncb.ernet.in/education/modules/dbms/SQL99/ansi-iso-9075-1-1999.pdf ISO/IEC 9075-1:1999 SQL Standard (pdf format)] . "Section 4.4.3.3". 1999.] . For DBMSs that implement the ISO SQL:1999 standard, the following code creates a table which holds instances of the Boolean data type.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.
Visual Basic
In
Visual Basic Boolean values from comparisons can be stored in variables with theBoolean
data type, which is stored as a 16-bit signed integer, but should only have the valuesTrue(-1)
andFalse(0)
. For example:Note: Although Boolean values should only be -1 or 0, other values can be coerced into them by calling a function with a
Variant
ByRef
parameter. It is highly recommended that you do not do this.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.
ee also
*true and false
shell scripting commands
*Shannon's expansion Notes and references
Wikimedia Foundation. 2010.