Boolean datatype

Boolean datatype

In computer science, the Boolean datatype, sometimes called the "logical datatype", is a primitive 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, or bit, and can also be represented in any other radix 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 and arithmetic.

Ada

Ada 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 (=, /=, &lt;, &lt;=, &gt;, &gt;=) 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.

Algol

Algol 60 had a Boolean datatype and associated operations, defined in the Algol 60 report. This was abbreviated to bool in ALGOL 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, a container::reference must be a true lvalue of type T. This is not the case with vector. Similarly, the vector::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 should be deprecated or entirely removed from the next version of the standard. [] >>> 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.>>>

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 = 0if (a) print "true"else print "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.classp true.classp nil.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.

CREATE TABLE test1 ( a int, b boolean);

INSERT INTO test1 VALUES (1, true);

INSERT INTO test1 VALUES (2, false);

INSERT INTO test1VALUES (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 test1VALUES (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.

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 BooleanisSmall = intMyNumber < 10 ' Expression evaluates to True or FalseIf isSmall Then MsgBox("The number is small")End If

Dim hellFreezesOver As Boolean ' Boolean variables are initialized as FalsehellFreezesOver = False ' Or you can use an assignment statementDo Call CheckAndProcessUserInput()Loop Until hellFreezesOver

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.Sub Voo(ByRef v As Variant) v = 1End Sub

Sub Bar(ByRef b As Boolean) b = 1End Sub

Dim b1 As Boolean, b2 As Booleanb1 = Trueb2 = TrueDebug.Print (b1 = b2) 'TrueCall Voo(b2)Debug.Print (b1 = b2) 'FalseCall 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.

ee also

*true and false shell scripting commands
*Shannon's expansion

Notes and references


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Boolean — (after George Boole), as a noun or an adjective, may refer to: * Boolean algebra (logic), a logical calculus of truth values or set membership * Boolean algebra (structure), a set with operations resembling logical ones * Boolean datatype, a… …   Wikipedia

  • Boolean domain — In mathematics and abstract algebra, a Boolean domain is a set consisting of exactly two elements whose interpretations include false and true . In mathematics and theoretical computer science, a Boolean domain is usually written as {0,1} or {ot …   Wikipedia

  • datatype — data type n. 1. In programming, a classification identifying one of various types of data, as floating point, integer, or Boolean, stating the possible values for that type, the operations that can be done on that type, and the way the values of… …   Universalium

  • Null (SQL) — The Greek lowercase omega (ω) character is used to represent Null in database theory. Null is a special marker used in Structured Query Language (SQL) to indicate that a data value does not exist in the database. Introduced by the creator of the… …   Wikipedia

  • C/AL — (Client Application Language) is the programming language used within the C/SIDE Integrated DevelopmentEnvironment in Microsoft Dynamics NAV (Formerly known as Navision Attain).C/AL is a database specific programming language, and is primarily… …   Wikipedia

  • EXPRESS (data modeling language) — EXPRESS is a standard data modelling language for product data. EXPRESS is formalized in the ISO Standard for the Exchange of Product model STEP (ISO 10303), and standardized as ISO 10303 11. Overview Data models formally define data objects and… …   Wikipedia

  • List of mathematics articles (B) — NOTOC B B spline B* algebra B* search algorithm B,C,K,W system BA model Ba space Babuška Lax Milgram theorem Baby Monster group Baby step giant step Babylonian mathematics Babylonian numerals Bach tensor Bach s algorithm Bachmann–Howard ordinal… …   Wikipedia

  • Propositional variable — In mathematical logic, a propositional variable (also called a sentential variable or sentential letter) is a variable which can either be true or false. Propositional variables are the basic building blocks of propositional formulas, used in… …   Wikipedia

  • Mogensen-Scott encoding — In computer science, Scott encoding is a way to embed inductive datatypes in the lambda calculus. Mogensen Scott encoding extends and slightly modifies this to an embedding of all terms of the untyped lambda calculus.DefinitionLet D be a datatype …   Wikipedia

  • Bit field — A bit field is a common idiom used in computer programming to store a set of Boolean datatype flags compactly, as a series of bits. The bit field is stored in an integral type of known, fixed bit width. Each Boolean flag is stored in a separate… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”