BlockchainSolidity7 min readUpdated

The bool Type in Solidity: True, False, and Safety Flags

By Mudassir Khan — Agentic AI Consultant & AI Systems Architect, Islamabad, Pakistan

Cover illustration for: The bool Type in Solidity: True, False, and Safety Flags

Section 01 · Definition

What is bool in Solidity?

bool is the boolean type. It can be true or false, and nothing else.

Quick answer

What is bool? bool is Solidity's boolean type. A bool variable holds one of two values — true or false — and supports the standard logical operators && (and), || (or), and ! (not). Every condition you write inside if, require, while, and the ternary operator is a bool expression.

solidity
bool public paused;             // default false
bool public initialised = true;
bool public whitelisted;

function unpause() external {
    paused = false;
}

Solidity does not implicitly convert other types to bool. Unlike JavaScript or Python, there is no truthy / falsy concept. You can never write if (counter) to mean "if counter is non-zero". The compiler will reject it. You write if (counter > 0). This rule eliminates an entire class of bugs.

Section 02 · Operators

The three boolean operators and a truth table

Solidity gives you the same logical operators every C-family language has — and they short-circuit, which has gas implications.

Truth table for boolean operators showing A, B, A AND B, A OR B, and NOT A combinations.
Standard truth table. Solidity short-circuits both && and || left to right.
solidity
bool isOpen = true;
bool isWhitelisted = whitelist[msg.sender];

// AND  — both must be true
if (isOpen && isWhitelisted) { ... }

// OR   — at least one must be true
if (isAdmin || isOwner) { ... }

// NOT  — invert
if (!paused) { ... }

// Short-circuit means cheapMath is NOT called when isOpen is false
require(isOpen && cheapMath(), "closed or invalid");

The short-circuit behaviour matters for gas. Put the cheaper or more often-false check on the left of an &&, and the cheaper or more often-true check on the left of an ||, so the expensive check runs as rarely as possible.

Section 03 · Storage

A bool still costs a full storage slot — unless you pack it

The EVM stores everything in 32 byte slots, so a bool by itself wastes 31 bytes. Pack bools alongside other small fields and the cost disappears.

solidity
// Wasteful: each bool takes its own 32 byte slot
bool public paused;          // slot 0  — 31 bytes wasted
bool public initialised;     // slot 1  — 31 bytes wasted
uint256 public counter;      // slot 2

// Packed: all three fit inside one 32 byte slot
struct Flags {
    bool paused;             // 1 byte
    bool initialised;        // 1 byte
    uint64 counter;          // 8 bytes
    // 22 bytes unused but only ONE storage slot
}
Flags public flags;

Inside a struct, the compiler packs adjacent small fields together. Outside a struct, two bools declared back-to-back at the contract level still take two slots. Read more about layout in the structs in Solidity guide.

Section 04 · Real-world uses

Where bool shows up in production code

Five patterns you will see in almost every contract.

solidity
// 1. Pause switch
bool public paused;

modifier whenNotPaused() {
    require(!paused, "paused");
    _;
}

// 2. One-time initialiser guard
bool private _initialised;

function init() external {
    require(!_initialised, "already initialised");
    _initialised = true;
    // ...
}

// 3. Whitelist mapping
mapping(address => bool) public allowedCallers;

// 4. Return value from a low-level call
(bool ok, ) = payable(to).call{value: amount}("");
require(ok, "transfer failed");

// 5. Bid-or-buy auction logic
bool public buyNowAvailable = true;

Pair bool flags with a modifier to enforce them across many functions, and emit a event whenever the flag changes so off-chain systems can keep up.

Default false is a safety property

Because uninitialised bools default to false, a contract is never accidentally 'paused = true' or 'whitelisted = true'. Design your flags so the safe state is the default. For example name a permission 'isFrozen' (default false = unfrozen) rather than 'isUnlocked' (default false = locked), so a bug never accidentally locks everyone out.

Section 06 · FAQ

Frequently asked questions

Does Solidity have truthy and falsy values like JavaScript?

No. Conditions must be of type bool. You cannot write `if (counter)` or `if (someAddress)`. You must write `if (counter > 0)` or `if (someAddress != address(0))`. The strictness eliminates an entire category of subtle bugs.

Is bool stored in 1 byte or 32 bytes?

On its own at the contract level, a bool occupies a 32 byte storage slot like every other state variable. Inside a struct, the compiler packs it into 1 byte if there is a small field next to it. Inside memory, bool always uses 32 bytes for alignment.

Can I XOR two bools in Solidity?

Yes — use the != operator. `a != b` returns true when exactly one of them is true, which is the XOR truth table. There is no dedicated `^^` operator for bools.

What is short-circuit evaluation?

When evaluating A && B, if A is false the answer is already false and Solidity skips B. When evaluating A || B, if A is true the answer is already true and Solidity skips B. Put the cheap or often-deciding check first to save gas.

Why does Solidity not have a Maybe / Optional type?

It does not have built-in algebraic data types. The closest pattern is returning (bool ok, T value) from a function — the bool indicates whether the operation succeeded and the value is only meaningful when ok is true. This is the same pattern Solidity itself uses for low-level .call return values.

Written by Mudassir Khan

Agentic AI consultant and AI systems architect based in Islamabad, Pakistan. CEO of Cube A Cloud. 38+ agentic AI launches delivered for global founders and CTOs.

View agentic AI consulting serviceSee ChainTrust case study

Related service

Agentic AI Consulting

See scope & pricing →

Related case study

ChainTrust Compliance Engine

Read case study →

More on this topic

Need an AI systems architect?

Book a 30-minute architecture call. I will sketch the high-level design for your use case and give you an honest view of the trade-offs.

Book a strategy call →