Boolean
Overviewβ
A Boolean is the simplest data type in computing. It represents truth values, usually true or false. Despite its tiny size, it quietly controls the flow of programs, like a traffic signal deciding who moves and who waits π¦.
The name comes from George Boole, whose logic laid the foundation for digital circuits and programming languages.
Possible Valuesβ
Most languages support exactly two values:
truefalse
Some low-level systems may represent these internally as:
1and0onandoffyesandno
But conceptually, itβs always a binary choice.
How It Worksβ
Booleans are commonly used in:
- Conditions (
if,else) - Loops (
while,for) - Logical expressions
- Flags and state tracking
At the hardware level, Boolean values map cleanly to electrical states, which is why they feel so natural in computers.
Logical Operationsβ
| Operation | Description | Example |
|---|---|---|
AND (&&) | True if both operands are true | true && false β false |
OR (||) | True if any one operands are true | true || false -> true |
NOT (!) | Inverts the value | !true β false |
| XOR | True if operands differ | true XOR false β true |
Exampleβ
Pseudocodeβ
isLoggedIn = true
if isLoggedIn:
showDashboard()
else:
showLoginPage()
Real-world analogyβ
A Boolean is like a door sensor:
- Door closed β
false - Door open β
true
No shades of grey. Just a clean decision.
Time and Space Complexity
- Space: O(1)
- Operations: O(1)
Booleans are lightweight and fast, the sprinters of data types πββοΈ.
Use Casesβ
- Feature toggles
- Permission checks
- Validation results
- Loop control
- Error flags
Advantagesβ
- Extremely memory efficient
- Easy to reason about
- Maps directly to logic and hardware
Limitationsβ
- Only represents binary states
- Not suitable for multi-valued conditions without combinations