Bitwise AND, OR, XOR: What the Bits Are Doing

Three tiny operations, AND, OR and XOR, sit underneath file permissions, network masks, game flags, checksums and every “is this option enabled?” check ever written. They look exotic in code and are almost embarrassingly simple once you watch them work on actual bits: each one walks down two binary numbers column by column and writes one answer bit per column, no carrying, no arithmetic. This guide makes the three of them concrete, with our free binary AND tool and bitwise hex calculator as the places to watch it happen.

Three rules, one worked example

Per column of bits: AND writes 1 only when both inputs are 1. OR writes 1 when at least one is. XOR writes 1 when exactly one is, which makes it the odd one out in both senses. Running all three on the same pair, hex CC and AA, shows their personalities side by side:

BinaryHex
input A1100 1100CC
input B1010 1010AA
A AND B1000 100088
A OR B1110 1110EE
A XOR B0110 011066

AND kept only the columns where both had a 1; OR collected every 1 from either side; XOR kept exactly the columns where the two disagreed. Every use in the rest of this guide is one of those three sentences wearing a job title. To follow along with your own values, the hex to binary converter spreads any hex number into visible bits first.

AND: the mask

AND’s superpower follows from its rule: anything ANDed with 0 dies, anything ANDed with 1 survives. Build a number whose 1-bits mark the positions you care about, the mask, and AND extracts exactly those positions from any value: C3 AND 0F = 03 keeps the low four bits and discards the high four, which is how code pulls a color channel out of a packed pixel or a field out of a packed header. The same move asks yes-no questions: Unix file permissions are bit flags, and “can the owner write?” is one AND between the mode and the write bit, with the whole permission-numbers system unpacked in the chmod guide. Network subnet masks are the third face: the mask’s 1-bits select the network part of an IP address, and AND does the selecting, identically on every router on earth.

OR: the switch-on

OR is AND’s complement in practice: where AND with a mask reads and clears, OR with a mask sets. Any bit ORed with 1 becomes 1, every other bit passes through untouched, so turning on the bold flag in a style word, or adding the read permission to a mode, is one OR with the right mask, regardless of what was already set. This is also why flag arguments combine with OR in so many APIs: each option is a number with a single distinct 1-bit, and OR merges any selection of them into one integer that AND can later interrogate option by option. A flags integer is a tiny database, OR is its insert, and AND is its query.

XOR: the difference detector

XOR answers “where do these differ?”, returning 0 for matching columns and 1 for mismatched ones, which makes A XOR B = 0 the cheapest possible equality test. From the same rule comes its strangest gift: applying the same XOR twice undoes it, since the differences cancel, CC XOR AA gives 66, and 66 XOR AA returns CC exactly. That round trip powers bit toggling (XOR with a mask flips just the masked bits), the classic XOR cipher (XOR with a key scrambles, the same key unscrambles), and parity-based recovery in RAID storage, where a drive’s contents can be rebuilt by XORing the survivors. One operation, and the common thread is always the same: XOR remembers differences, and differences applied twice vanish.

Counting ones: the bit census

After combining bits, the natural follow-up question is how many survived. The count of 1-bits in a value, the population count, is its own small institution: B7 is 1011 0111 in binary and contains 6 ones, so its parity is even, the one-bit summary that hardware has used for error checking since the punch-card era. Combined with XOR it becomes a distance: the number of ones in A XOR B is the Hamming distance, exactly how many bit positions separate two values, which error-correcting codes are built around. The one-bit counter does the census instantly, and the rest of the number-system story, bases, bytes and conversions, lives in the number systems pillar.

Frequently asked questions

What is the difference between bitwise AND and logical AND?

Bitwise AND (& in most languages) works column by column across all bits and returns a number; logical AND (&&) treats each whole value as true or false and returns a truth value. Mixing them up compiles fine and misbehaves quietly, which is why the distinction earns its place in code reviews.

Is there a bitwise NOT?

Yes: NOT flips every bit of a single value, turning each 0 into 1 and back. Its everyday use is building masks, where NOT of “the bits I want gone” gives “everything else”, and AND with that clears the targets. On fixed-width numbers NOT also connects to negative values, a story that belongs to two’s complement.

Why do these operations show up in “clever” code tricks?

Because they cost almost nothing: a bitwise operation is among the cheapest things a processor does, with no carries or branches. Tricks like testing evenness with AND 1, or swapping variables with three XORs, exploit that. The honest advice: know them to read them, and write the clear version unless profiling says otherwise.

Do bitwise operations work on hex and decimal numbers too?

They work on the bits underneath, whatever base displayed the number. Hex is simply the convenient costume, since each hex digit is exactly four bits; that is why masks read so cleanly in hex (0F, FF00) and why the hex calculator takes hex in and gives hex out while the real work happens in binary.

ATV

Written by Nick (ATV Team)

We build and maintain the 600+ free, client-side tools on this site, and every guide is written against the tools themselves: each figure is computed and checked before it is published, and every linked tool is tested in the browser. More about how we work on the about page, and the full library of guides lives on the blog.