Computer science reference

Logic Operators Guide

Understand AND, OR, NOT, XOR, NAND, NOR, and related operators across truth tables, Boolean algebra, digital circuits, programming, and AI prompts.

Basic logic

NOT

Notation
¬A, A'
Programming
Circuit
Inverter gate

Example

Reverses a truth value.

If A means logged in, ¬A means not logged in.

Truth table

AOut
01
10

Basic logic

AND

Notation
A·B, AB
Programming
Circuit
AND gate

Example

True only when every input is true.

isLoggedIn && hasPermission

Truth table

ABOut
000
010
100
111

Basic logic

OR

Notation
A+B
Programming
Circuit
OR gate

Example

True when at least one input is true.

isAdmin || isOwner

Truth table

ABOut
000
011
101
111

Derived operators

XOR

Notation
A⊕B
Programming
Circuit
XOR gate

Example

True when the inputs are different.

Half adder sum = A XOR B.

Truth table

ABOut
000
011
101
110

Derived operators

NAND

Notation
¬(A·B)
Programming
Circuit
NAND gate

Example

The negation of AND. NAND gates can build any Boolean circuit.

A NAND B = NOT (A AND B).

Truth table

ABOut
001
011
101
110

Derived operators

NOR

Notation
¬(A+B)
Programming
Circuit
NOR gate

Example

The negation of OR. NOR is also functionally complete.

A NOR B is true only when both inputs are false.

Truth table

ABOut
001
010
100
110

Derived operators

XNOR

Notation
¬(A⊕B)
Programming
Circuit
XNOR gate

Example

True when the inputs are the same.

A XNOR B behaves like equality for Boolean values.

Truth table

ABOut
001
010
100
111

Bitwise operators

Bitwise AND

Notation
bit mask
Programming
Circuit
Per-bit AND operation

Example

Applies AND to each bit position. It is different from logical &&.

0101 & 0011 = 0001

Programming

Short-circuit evaluation

Notation
evaluation rule
Programming
Circuit
Programming evaluation behavior

Example

The second expression may not run if the first expression already determines the result.

user && user.name

Boolean algebra laws

Identity laws

A ∧ 1 = A, A ∨ 0 = A

Combining with the neutral truth value leaves A unchanged.

Domination laws

A ∧ 0 = 0, A ∨ 1 = 1

One fixed input can determine the entire result.

Complement laws

A ∧ ¬A = 0, A ∨ ¬A = 1

A statement and its negation cannot both be true, but at least one is true.

De Morgan's laws

¬(A ∧ B) = ¬A ∨ ¬B, ¬(A ∨ B) = ¬A ∧ ¬B

Moves a negation across AND or OR while switching the operator.

Absorption laws

A ∨ (A ∧ B) = A, A ∧ (A ∨ B) = A

A repeated condition can absorb a more specific condition.