DEV Community

dayu2333-jinyul
dayu2333-jinyul

Posted on

Understanding Bitwise Operations — AND, OR, XOR, NOT, and Shifts

A few weeks ago I was debugging a network protocol implementation and kept flipping between binary calculators trying to figure out which bits were where. The operations themselves are simple — AND, OR, XOR, NOT, shift — but keeping 32 bits straight in your head while thinking about masking and flags is harder than it looks.

Here is the cheat sheet I wish I had.

The Operations

AND (&)

Returns 1 only when both bits are 1. Everything else becomes 0.

0101  (5)
0011  (3)
---- AND
0001  (1)
Enter fullscreen mode Exit fullscreen mode

Real use: Bit masking. If you want to extract the lowest byte of a 32-bit value, you AND with 0xFF. The upper 24 bits get zeroed out.

Try it: AND Calculator

OR (|)

Returns 1 when at least one bit is 1.

0101  (5)
0011  (3)
---- OR
0111  (7)
Enter fullscreen mode Exit fullscreen mode

Real use: Setting bits without touching the rest. If READ=1, WRITE=2, EXECUTE=4, then flags | READ | WRITE gives you read+write without affecting execute.

Try it: OR Calculator

XOR (^)

Returns 1 when the two bits differ.

0101  (5)
0011  (3)
---- XOR
0110  (6)
Enter fullscreen mode Exit fullscreen mode

Real use: The foundation of encryption. XORing a value twice with the same key restores the original — A ^ K ^ K = A. This is the core of stream ciphers and one-time pads.

Try it: XOR Calculator

NOT (~)

Flips every bit. 0 becomes 1, 1 becomes 0.

0101  (5)
---- NOT
1010  (-6 in two complement)
Enter fullscreen mode Exit fullscreen mode

Real use: ~x + 1 gives you -x in two complement. Also useful for clearing bits: value & ~mask clears the bits in mask.

Left Shift (<<)

Shifts all bits left, fills with zeros from the right.

0001  (1)
---- << 3
1000  (8)
Enter fullscreen mode Exit fullscreen mode

Real use: 1 << N builds a mask with a 1 at bit position N. Also the fastest way to multiply by powers of 2.

Right Shift (>>)

Shifts bits right, propagates the sign bit from the left.

1000  (8)
---- >> 3
0001  (1)
Enter fullscreen mode Exit fullscreen mode

Real use: Extracting specific bit fields. (value >> offset) & mask is standard practice in protocol parsing.

Try it: Bit Shift Calculator

Why This Matters

If you write systems code, embedded firmware, or anything near hardware, bitwise operations are unavoidable. Even higher up — flags, permissions, and compression all rely on them.

  • Linux file permissions: chmod 755 = owner rwx (4+2+1=7), group rx (4+1=5), other rx (4+1=5). Each permission is a bit.
  • Database bitmasks: A single integer column tracks multiple permissions. SELECT=1, INSERT=2, UPDATE=4, DELETE=8.
  • Network protocols: IP headers, TCP flags — every field is bits at specific positions.

If any of these click better when you can see the bits move, I built bitwisecalc.com with full 32-bit step-by-step visualization for each operation. The AND/OR/XOR/shift pages show every bit aligned and highlighted so you can trace exactly what is happening.

Top comments (0)