4. Introduction to Data Types

4.2. Boolean

A Boolean data type represents two values: true or false.

Characteristics

Storage: Typically 1 bit (theoretically) but often stored as a byte for alignment in memory.

Operations: Logical operations like AND (&&), OR (||), NOT (! or not).

Immutable: Booleans are simple and cannot hold other values.

Use Cases

Conditional statements (e.g., if statements).

Flags to control program flow (e.g., is_active = true).

Logical comparisons (e.g., x > y).

Examples

Python: is_valid = True, has_error = False

Java: boolean isValid = true;

C++: bool isValid = true;

Notes

In C, there’s no built-in Boolean type (uses 0 for false, non-zero for true), but C99 introduced stdbool.h.

Python’s bool is a subclass of int (True == 1, False == 0).