0Pricing
Coding Interview Prep · Lesson

AND, OR, XOR & Shifts

Read and combine bits with confidence.

AND, OR, XOR & Shifts is a free Coding Interview Prep lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Bits Are Your Toolkit

Every integer is really a row of bits, just 0s and 1s. Learning to read and combine them gives you fast, clever tricks in contests. 🔧

See the Binary

Use Python's bin() to peek at the bits of any number. The 0b prefix just marks it as binary, the rest is the bit pattern.

print(bin(13))  # 0b1101

AND Keeps Common Bits

The AND operator (&) gives a 1 only where both numbers have a 1. It is perfect for checking or masking shared bits.

print(6 & 3)  # 0b110 & 0b011 = 0b010 = 2

OR Combines Bits

The OR operator (|) gives a 1 wherever either number has a 1. Use it to switch flags on without disturbing the rest.

print(6 | 1)  # 0b110 | 0b001 = 0b111 = 7

XOR Spots Differences

The XOR operator (^) gives a 1 where the two bits differ. It is a contest favorite for toggling and finding the odd one out.

print(6 ^ 3)  # 0b110 ^ 0b011 = 0b101 = 5

XOR Cancels Itself

A number XOR itself is always 0, and XOR with 0 leaves it unchanged. That self-canceling property powers many XOR puzzles.

print(7 ^ 7)  # 0
print(7 ^ 0)  # 7

Find the Unique Number

If every value appears twice except one, XOR them all together and the pairs vanish. What survives is the unique value.

ans = 0
for x in [4, 1, 2, 1, 2]:
    ans ^= x
print(ans)  # 4

Left Shift Multiplies

A left shift pushes bits up, adding zeros on the right. Each shift by one doubles the value, like multiplying by a power of two.

print(3 << 2)  # 3 * 4 = 12

Right Shift Divides

A right shift drops bits off the right end. Each shift by one halves the value, doing integer division by a power of two.

print(20 >> 2)  # 20 // 4 = 5

Make a Power of Two

Shifting 1 left by k builds the value 2 to the k instantly. This is the cleanest way to create single-bit masks.

print(1 << 5)  # 32

Speed Matters in Contests

Bitwise operators run in a single fast step, so swapping arithmetic for shifts can rescue a borderline time limit. Know them cold. ⚡

Quick Check

Time to test your bit operators.

Recap: Bit Operators

You met AND, OR, XOR and shifts: combine, toggle, and scale bits in one fast step. These are the building blocks for every bit trick ahead. 🎉

Frequently asked questions

Is the “AND, OR, XOR & Shifts” lesson free?

Yes — the full text of “AND, OR, XOR & Shifts” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “AND, OR, XOR & Shifts”?

Read and combine bits with confidence. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “AND, OR, XOR & Shifts” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. AND, OR, XOR & Shifts
  2. Set, Clear & Toggle a Bit
  3. Count Bits and Lowest Set Bit
  4. Bitmasks as Tiny Sets
← Back to Coding Interview Prep