Skip to main content

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:

  • true
  • false

Some low-level systems may represent these internally as:

  • 1 and 0
  • on and off
  • yes and no

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​

OperationDescriptionExample
AND (&&)True if both operands are truetrue && false β†’ false
OR (||)True if any one operands are truetrue || false -> true
NOT (!)Inverts the value!true β†’ false
XORTrue if operands differtrue 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