Boolean data type
What is boolean data type?
Data type that has one of two possible values, usually denoted as true
and
false
. Which is intended to represent the two truth values of logic and
Boolean algebra.
It is named after George Boole 1, who first defined an algebraic system of logic in the mid 19th century.
conditional statements, which allow different actions by changing control flow depending on whether a programmer-specified Boolean condition evaluates to true or false. It
Boolean data type primary associated with conditional statemens, which implement different actions behaviour with control flow.
If there is no explicit Boolean data type (like in C90
), can boolean data
type be represented?
Yes usually used int
to represent true
(0) and false
(1) values.
The implementation of Booleans in computers are most likely represented as a
full word or a bit?
Usually as full word, this is usually due to the ways computers transfer
blocks of information.
Most programming languages, have support for Boolean algebraic operations such as:
Conjunction (AND, &, *): returns true
if both operands are ==true
==.
Disjunction (OR, |, +): returns true
if at least one operand is ==true
==.
Equivalence (EQV, =, ==): returns true
if both operands are the same.
OR - true
when one or both inputs are true, XOR returns true
when?
When inputs are different.
Exclusive OR (XOR), true
only if inputs differ.
print(True ^ False, False ^ True, True ^ True, False ^ False)
print(True or False, False or True, True or True, False or False)
Negation (NOT, !): Returns the opposite of the operand. NOT true and NOT false
will return ==false
==.
Boolean expression
Expression which produce a Boolean
value when evaluated. Used in many
programming languages as built-in type.
In C you need to include stdbool.h
to use bool
type.
#include <stdbool.h>
printf("true: %d\n", true); // true: 1
printf("false: %d\n", false); // false: 0