Each bit position is handled independently. 1100 AND 1010 is 1000, because only the fourth column has a 1 in both. There are no carries, so unlike addition a bitwise operation cannot propagate — and that is what makes it a single fast instruction.
Why the width matters
For AND, OR and XOR the width is cosmetic. For anything involving inversion it is not.
NOT flips every bit within the register. Inverting 12 gives 243 at 8 bits and 4294967283 at 32 — and both are correct. The operation is not defined without knowing how many bits there are.
The same applies to NAND, NOR and XNOR, since each is an inversion of something else.
This is a real source of bugs when code moves between types. Inverting a value stored in a smaller type and assigning it to a larger one gives a different answer from inverting it after the conversion, and neither is wrong — they are answers to different questions.
This page makes the width an explicit choice rather than picking one silently.
What each one is for
Each operation has a job it is reached for, and knowing which makes bit manipulation readable.
AND masks. A 1 in the mask keeps a bit, a 0 clears it. Testing a single flag is an AND with a one-bit mask.
OR sets. It turns bits on without disturbing the others, which is how flags are combined into a single value.
XOR toggles, and detects difference. It is zero exactly when the two operands are identical.
NOT inverts, and is usually seen building a mask: NOT of a bit pattern gives the mask that clears exactly those bits.
NAND and NOR are rarely written in software and are the fundamental gates in hardware, for the reason below.
XOR is special
XOR has a property the others lack: it is its own inverse. Apply it twice with the same value and you get back where you started.
That single fact accounts for most of what XOR is used for.
Toggling. XOR with a mask flips exactly those bits, and doing it again restores them.
Swapping without a temporary. Three XORs exchange two variables. It is a party trick rather than good practice — a compiler produces better code from an ordinary swap — but it demonstrates the property.
The one-time pad. XOR a message with a truly random key of the same length and the result is provably unbreakable. XOR it again with the same key and the message returns. It is the only cipher with a proof of perfect secrecy, and the difficulty is entirely in distributing keys as long as the messages.
Parity and error detection. XOR of all the bits is the parity bit, and RAID storage reconstructs a lost drive by XOR-ing the others.
NAND builds everything
NAND is functionally complete: every other logic operation can be built from it alone.
NOT is a NAND with both inputs tied together. AND is a NAND followed by that NOT. OR is a NAND of two NOTs. From there, everything.
So an entire processor can be made from nothing but NAND gates — and in a real sense one is. In CMOS, NAND and NOR are the cheapest gates to fabricate, and AND and OR are built from them rather than the other way round.
NOR is functionally complete too, and it has a claim to fame: the Apollo Guidance Computer, which flew to the Moon, was built entirely from NOR gates — about 2,800 of them in a single circuit type.
Masks and flags
The commonest practical use of bitwise operations is packing several yes/no values into one number.
Set a flag: value OR mask. Clear one: value AND NOT mask. Toggle one: value XOR mask. Test one: value AND mask, and check whether the result is non-zero.
Those four idioms cover nearly all bit manipulation in practice, and they are worth recognising on sight.
Unix file permissions are the familiar example: 755 in octal is three groups of three bits, and chmod is doing exactly these operations.
Network subnet masks are the same idea. An address AND a mask gives the network portion, which is how routing decides where a packet goes.
Sources and methodology
Bitwise logic is standard; these are the references.
Method. The register width is taken as an input rather than assumed, because NOT and the inverted operations genuinely depend on it — inverting 12 at 8 bits gives 243 and at 32 bits gives 4294967283, and both are correct for their width. All arithmetic is on BigInt, so a 64-bit operation is exact rather than being done in a double. The suite re-derives AND, OR and XOR against JavaScript’s own operators at 32 bits. That engine is verified on every change against 90 hand-written assertions, including that AND, OR and XOR match JavaScript’s own operators, and that XOR applied twice with the same key returns the original on four hundred generated pairs. The count and the per-case breakdown are published on the formula verification page.
Read the guide
Shifting is the other half of bit manipulation — the Bit Shift Calculator covers the two right shifts and why both exist.