Bitwise AND Tutorial

Masking, filtering, and flag checking — the three things every programmer uses AND for

Of all six bitwise operators, AND is the one you'll reach for most often. It answers one question: are these specific bits set? That single capability powers permission systems, network subnet masks, pixel color extraction, and hardware register reads. Here's how it works and how to use it.

How Bitwise AND Works

The AND operator (& in C/Java/JS/Python) compares two numbers bit by bit. A result bit is 1 only when both corresponding input bits are 1. Every other combination produces 0. This makes AND perfect for filtering — you create a mask with 1s in the positions you care about and 0s everywhere else, then AND it with your value. The result preserves only the bits where the mask had 1s.

Example: value = 0b1101_0110 (214). You want only the lower 4 bits. Create mask 0b0000_1111 (15). 214 & 15 = 6. The upper 4 bits are zeroed out — filtering complete. Try this on our bitwise AND calculator to see the binary columns line up.

Pattern 1: Checking If a Specific Bit Is Set

The most common AND pattern in any codebase. You have a value and want to know: is bit N a 1 or a 0? Create a mask with a 1 at position N: mask = 1 << N. AND it with the value. If the result is non-zero, the bit is set.

# Check if bit 3 is set in value 42 (0b00101010)
value = 42
mask = 1 << 3   # 0b00001000 = 8
if value & mask:
    print("Bit 3 is set")   # 42 & 8 = 8 → True
else:
    print("Bit 3 is clear")

This pattern appears everywhere: file permission checks (mode & S_IRUSR in C), feature flags (features & FEATURE_DARK_MODE), and hardware status registers (STATUS_REG & UART_TX_READY).

Pattern 2: Extracting a Bit Field

Often multiple bits together represent a value — like the red channel in a 32-bit ARGB pixel (bits 16-23), or the day-of-month field in a packed date. AND isolates the field, then shift brings it down to an interpretable range.

# Extract green channel from ARGB pixel (bits 8-15)
pixel = 0xFF_7B_2C_11   # ARGB: alpha=FF, red=7B, green=2C, blue=11
green = (pixel >> 8) & 0xFF  # → 0x2C = 44
# Step 1: shift right 8 to move green into lowest byte
# Step 2: AND with 0xFF to clear everything above byte 0

This two-step extraction — shift then mask — is the standard idiom. Without the final AND, bits from alpha and red would contaminate your result. Use our bitwise calculator to trace the bits through each step.

Pattern 3: Clearing Specific Bits

To turn off particular bits while leaving others unchanged, AND with a mask that has 0s in the positions you want to clear and 1s everywhere else. This is one of the few times you'll build a mask with NOT: value & ~(1 << N) clears bit N.

Pattern 4: Modulo for Powers of Two

x % (2^n) is equivalent to x & ((2^n) - 1). For example, x % 8 equals x & 7. This is faster on most CPUs and appears in hash table implementations, ring buffers, and circular array indexing. The compiler often does this optimization automatically, but knowing the equivalence helps you read optimized disassembly.

Pattern 5: Subnet Mask Matching

Every IP packet routed on the internet passes through a bitwise AND. A subnet mask like 255.255.255.0 (binary: 24 ones followed by 8 zeros) is AND'd with an IP address to determine the network portion. Two IPs on the same subnet produce the same AND result. 192.168.1.50 & 255.255.255.0 = 192.168.1.0. Routers use this to decide whether to forward a packet locally or to the gateway.

AND is deceptively simple — one truth table, four rows — but it's the bitwise operator you'll use most. Master these five patterns and you'll start seeing opportunities for bit-level optimization everywhere. Next: read our guide on OR and XOR to complete the bitwise toolkit.