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.
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.
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.
// 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.
// 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.