article

In computer science the boolean datatype, sometimes called the logical datatype, is a primitive datatype having two values: one and zero (sometimes called true and false). 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.

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 (=, /=, <, <=, >, >=) 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.

C


In the C programming language, there is no boolean type provided in the C89 (but there is one in C99), but instead true/false values are determined by comparing a value to zero. For instance, the C code

if (my_variable) { printf("True!\n"); } else { printf("False!\n"); }

does the exact same thing as

if (my_variable != 0) { printf("True!\n"); } else { printf("False!\n"); }

This is straightforward for integer datatypes; however, binary floating-point values are approximations of displayed decimal values and so should not normally be compared for equality. Traditionally, integers are used to contain one (or more) boolean variables, one for each digit of the integer.

While it is not necessary to name the true and false values in order to test variables for truth or falsehood, it is necessary to do so in order to assign values to them. (One way is to use the values zero and one, which have the advantage of being language-independent.) Alternatively, the enum keyword allows for naming elements in the language of your choice, for example:

typedef enum _boolean { FALSE, TRUE } boolean; ... boolean b;

However, enum is seldom used for this; instead, the 0 and 1 values are used with the following typical preprocessor macros.

#define FALSE 0 #define TRUE 1 ... int f = FALSE;

On a recent C compiler (supporting the C99 standard), there is a _Bool type, which is used to define bool by the stdbool.h header:

#include bool b = false; ... b = true;

C++


During its standardization process, the C++ programming language introduced the bool, true and false keywords, adding a native datatype to support boolean data.

Preprocessor macros may be used to turn bool into _Bool, false into 0 and true into 1, allowing compatibility with the aforementioned C99 use of the stdbool.h header.

C#


In C#, Boolean variables are represented by the type bool, which occupies one byte.

Code to output a boolean could be represented like this:

bool myBool = (i == 5); System.Console.WriteLine(myBool ? "I = 5" : "I != 5");

Fortran


The LOGICAL keyword and associated operations .NOT., .AND., .OR., etc. were introduced in the 1950s, before Fortran was standardized.

Java


In the Java programming language, boolean variables are represented by the type boolean, which typically occupies one byte (the actual representation is left to the JVM). No explicit or implicit casts to or from boolean are permitted. The code

int i = 5; if (i) System.out.println("i is five");

causes compilation errors. Code to output a boolean could be represented as

boolean myBool = (i == 5); System.out.println("i == 5 is " + myBool);

which outputs:

i == 5 is true

Lambda Calculus


In the Lambda Calculus formal model of computing, booleans are represented as Church booleans.

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

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.

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 string "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".

Aggregate types may also be tested against "existence" or "non-existence"and all variables may be evaluated as either "defined" or "undefined".[http://perldoc.perl.org/functions/defined.html 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.

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) print "true" else print "false" end

will print "true", which might come as a surprise to a new user of the language.

Visual Basic


In the Visual Basic programming language, boolean values from comparisons can be stored in variables with the Boolean data type, which is stored as a two-byte integer, but can only have the values True and False. For example:

Dim isSmall As Boolean isSmall = intMyNumber < 10 ' Expression evaluates to True or False If isSmall Then MessageBox.Show("The number is small") Endif

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

boolean algebra | Data types

Variabile booleana | Булевский тип

 

This article is licensed under the GNU Free Documentation License. It uses material from the "Boolean datatype".

Home Pageartsbusinesscomputersgameshealthhospitalshomekids & teensnewsphysiciansrecreationreferenceregionalscienceshoppingsocietysportsworld